I’d like to sort an array of strings case-insensitively.
Alternate method (slightly “faster”, maybe):
a = %w(a c b A C B)
a.sort {|a,b| a.casecmp b} #=> [“a”, “A”, “b”, “B”, “c”, “C”]
Actually, I want [“A”, “a”, “B”, “b”, “C”, “c”] .
I can get the result with the following trick.
a.sort_by {|i| i.upcase + i}
Clever.
But I think there’s a more rubyish way.
How do you do that?
No clue. Maybe implement your own comparator? {|a,b| dict_order(a,b)}?
But I think there’s a more rubyish way.
How do you do that?
Hi Sam,
I think that’s probably the most concise and rubyish way. You can’t do
a (simple) sort by ascii ordinal (i[0] in 1.8, or i.ord in 1.9), as
that would give you all the capitals first. And implementing your own
sort routine is not going to be anywhere near as clean as what you’ve
got. So yeah, imo, go with what you have, it’s nice.