Well, from your question that you are doing, you really wanted to group the results by value :accepts and combine both result sets back into one array. My solution for this would be:
array.select {|where| where[:accepts] } | array.reject {|where| where[:accepts] } # => [{:accepts=>true, :id=>3}, # {:accepts=>true, :id=>5}, # {:accepts=>false, :id=>1}, # {:accepts=>false, :id=>2}, # {:accepts=>false, :id=>4}]
This will maintain the original order without specifying a type :id . This means that you do not need an auxiliary key to maintain order, and you can keep the order of the result regardless of the data transferred.
This may be useful (and perhaps exactly what you need for further evaluations):
array.group_by {|where| where[:accepts] } # => {false=>[{:accepts=>false, :id=>1}, # {:accepts=>false, :id=>2}, # {:accepts=>false, :id=>4}], # true=>[{:accepts=>true, :id=>3}, # {:accepts=>true, :id=>5}]}
Again, there were no artificial varieties ... group_by is new in 1.8.7.
PS: If you do not want the first code fragment to remove duplicates from your array, replace the panel operator with the plus operator. "|" combines two sets in accordance with the theory of set theory (union), while "+" combines two sets (the result is not really a set, but a simple array).
hurikhan77
source share