Reputation:
I am trying to sort string elements in array. Let's say I want to sort it [b, e, f, t, g], but want to keep e in first in ruby. So the result should be [e, b, f, g, t]. How can I write a code in ruby that sort the array that way.
Upvotes: 3
Views: 1295
Reputation: 29373
You could partition the Array
into all the "e"'s and all the not "e"'s then reduce them back together like so
arr = ["b", "e", "f", "t", "e","g"]
arr.partition {|e| e == 'e'}.reduce {|e,not_e| e.concat(not_e.sort)}
#=> ["e", "e", "b", "f", "g", "t"]
Additional credit to @iGian for
arr.sort.partition {|e| e == 'e'}.flatten
#=> ["e", "e", "b", "f", "g", "t"]
Upvotes: 2
Reputation: 11193
Just to add one option more, converting to an Hash:
ary = [:b, :e, :f, :t, :g, :e]
ary.sort.group_by(&:itself).then { |h| h.delete(:e) + h.values.flatten }
It returns the array keeping all :e
in front. :e
must exist.
#=> [:e, :e, :b, :f, :g, :t]
The first part is returning the sorted grouping hash:
ary.sort.group_by(&:itself) #=> {:b=>[:b], :e=>[:e, :e], :f=>[:f], :g=>[:g], :t=>[:t]}
:e
is not present:
ary.sort.group_by(&:itself).then { |h| h.delete(:e).to_a + h.values.flatten }
Since nil.to_a #=> []
Upvotes: 1
Reputation: 559
You can try this:
arr.sort.reject{|x| x=="e"}.unshift('e')
If you have more than "e" in the array then it will ignore all 'e'.
Upvotes: 0
Reputation: 110685
["b", "e", "f", "t", "g"] .sort_by { |s| [s == 'e' ? 0 : 1, s] }
#=> ["e", "b", "f", "g", "t"]
Here Enumerable#sort_by uses Array#<=> to compare each pair of elements computed by the block, such as [0, "e"]
and [1, "b"]
. See the third paragraph of the doc for Array#<=>
.
Upvotes: 5