Why ruby sort array of strings differently than sql order (postgres)?

Viewed 570

When the code would tell better than words:

[4] pry> Models::Company.order(:name).select_map(:name)
=> ["Dekker, Jacobs and Bosch", "Koster en Zonen", "Peters-Koster", "Peters, Vries and Smits", "Wal, Linden and Jacobs"]
[5] pry> Models::Company.order(:name).select_map(:name).sort
=> ["Dekker, Jacobs and Bosch", "Koster en Zonen", "Peters, Vries and Smits", "Peters-Koster", "Wal, Linden and Jacobs"]

Because of this my spec randomly failing. I can normalize arrays before comparing them, like this:

result_names.map { |s| s.gsub(/,|-/, '') }

I tried to sort the same array in JavaScript, and got the same result as Ruby gave me. I am wondering now, is there some sort of standard for sorting strings, or is it the detail of implementation?

1 Answers
Related