Sort values using a specific collation in Ruby/Rails -


Is it possible to sort an array of values ​​using specific matching in Ruby? I need to sort according to da_DK collation.

Viewing the array % w (Aarhus Aalborg Asance) me ['Ascence', 'Aalborg', ' Aarhus'] is to return which is the correct sequence in Danish.

The standard sort method

 % w (Aarhus Aalborg Asans) .sort   

Returns is something that is an ASEX command Looks less like Denmark's order):

  ["Aalborg", "Aarhus", "Asance"]   

Both the snow leopard and the environment Linux running Ruby 1.9.2 and Rail 3.0.5. According to

:

In Danish and Norwegian Alphabets, Swedish The same additional tone (see below) also exists, but in a different order and different glyph (..., x, y, z, one ??, a ??, a ??) "The word" a ?? " The Danish alphabet is traditionally seen as "W" as a version of "V", but today "W" is considered a separate letter.

This will stop sorting.

To fix this problem:

  name =% w (Aarhus Aalborg Assenses). Sertibine | | | w.gsub ('A', 'a ??) # = & gt; ["essen", "elborg", "ahus"]   < P> And there are some similar characters for other characters that have mixed letter combinations to convert to the same letter.  

The reason it works is sort_by , so it Really sorted by return value returned from block In this case, 'A' a ?? 'replacement is temporary, and is discarded when the array is sorted.

sort_by is very powerful , But there is some overhead. Because it's fast. For the type where you're comparing two simple values ​​at the top level of an object, it becomes a wash or not You should use sorting or sort_by . If there is a complex calculation or to move around in an object, then sort_by can prove to be fast, there is no real hard and fast way to know which is better, so I firmly Recommend testing with benchmark if you have to sort out large orange or have to deal with objects because the difference can be large, and sometimes the sort can be a better option.

Edit:

Ruby, by itself, is not doing what you want, because there is no knowledge of sort order, set out of each character there. It has been told about why it is. If you want an ICU qualification, you can see. I have not played with it, but it seems like your only real solution in Ruby.

You may be able to do something from a database like Postgres. They support different coating options, but usually you force the database to declare during the preparation ... or maybe it happens when the table is formed ... since I have created a new table anyway , This would be an option, although it would be a pain.

Comments