This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Ignoring Case When Sorting Strings | ||
|---|---|---|
| Code | Expected | Actual |
list = ["Albania", "anteater", "zorilla", "Zaire"] list.sort |
["Albania", "Zaire", "anteater", "zorilla"] | ["Albania", "Zaire", "anteater", "zorilla"] |
list.sort_by { |x| x.downcase } |
["Albania", "anteater", "Zaire", "zorilla"] | ["Albania", "anteater", "Zaire", "zorilla"] |
list.collect { |s| [s.downcase, s] }.sort.collect { |subarray| subarray[1] }
m = {}
list.sort { |x,y| (m[x] ||= x.downcase) <=> (m[y] ||= y.downcase) } |
["Albania", "anteater", "Zaire", "zorilla"] | |