Thanks to the help in a previous question, I've got the following code returning the expected results with find_all.
Original array (sorted by votes[0]):
votes_array = [{"votes"=>[13], "id"=>"4", "elected"=>0}, {"votes"=>[12], "id"=>"1", "elected"=>0}, {"votes"=>[8], "id"=>"3", "elected"=>0}, {"votes"=>[3], "id"=>"2", "elected"=>0}, {"votes"=>[3], "id"=>"0", "elected"=>0}]
And the code that runs after this array:
grouped_last = []
grouped_last = votes_array.find_all {|i| i["votes"][0] == votes_array.last["votes"][0] }
if grouped_last.length > 1
grouped_last.each do |candidate|
candidate["votes"][0] += 10 # another value is actually inserted here
end
end
The problem is that it's affecting the original array - as I'm thinking it should, as find_all is just a filter.
Is there a way to perform the same function as the find all with map/collect so that it creates a new array that doesn't affect the original? My brain is fizzing trying to work this out today!
EDIT: Let's see if I can clarify this a bit.
I'm using the votes_array to store a set of values for each stage of a voting process (using Single Transferrable Vote) per candidate.
For each round the candidate with the lowest votes is eliminated, and the next preference votes for that candidate are transferred to the relevant candidates ready for the next round.
However if there is more than one candidate with the same lowest votes, then the next preference votes for each candidate are compared and the lowest number of candidates votes signals the eliminated candidate.
What I'm trying to do with this part is to find the candidate to eliminate.
I'm wanting to create a new array containing the lowest vote candidates from the original array, then add a value (determined by another mapped array provided somewhere else).
Once I then have grouped_last with the new values I'll sort and analyse again for last position.
The problem is that I want grouped last to be a standalone array and not affect the original array/hash.