44

When I wrote in JavaScript "Ł" > "Z" it returns true. In Unicode order it should be of course false. How to fix this? My site is using UTF-8.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Tomasz Wysocki
  • 11,170
  • 6
  • 47
  • 62
  • What are you trying to do exactly? Maybe there are workarounds. – Pekka Sep 02 '10 at 19:51
  • I'm trying to sort table based on user names and I have letters like "Ł". – Tomasz Wysocki Sep 02 '10 at 19:56
  • In other words, it must come right after `L`? I.e. `..J,K,L,Ł,M,N,O..`? – BalusC Sep 02 '10 at 20:12
  • Yes. You can find full sequence for Polish here: http://en.wikipedia.org/wiki/Polish_alphabet . – Tomasz Wysocki Sep 02 '10 at 20:33
  • 6
    The term you're looking for is "collation", and it is notoriously hard. There is no such thing as "Unicode order"; Unicode explicitly recognizes the fact that different locales have different orders. See http://unicode.org/reports/tr10/ - "does not provide for the following features: ... Linguistic applicability" – MSalters Sep 03 '10 at 07:26
  • Also, is `"Ła" > "Le"` ? – MSalters Sep 03 '10 at 07:38
  • 1
    I have changed “UTF-8” in the question title to “Unicode”, since the issue does not depend on a particular transfer encoding. (Besides, JavaScript internally uses UTF-16, not UTF-8, even if the HTML document’s encoding is UTF-8.) – Jukka K. Korpela May 12 '14 at 19:04
  • Well there is a Unicode order, but it won't match Polish order. – hippietrail Apr 03 '15 at 11:45

6 Answers6

39

You can use Intl.Collator or String.prototype.localeCompare, introduced by ECMAScript Internationalization API:

"Ł".localeCompare("Z", "pl");              // -1
new Intl.Collator("pl").compare("Ł","Z");  // -1

-1 means that Ł comes before Z, like you want.

Note it only works on latest browsers, though.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • If you have to sort a list of multi-locale words. For instance a list of people names from various countries. This will not work, isn't it ? – Mic Oct 22 '18 at 13:56
19

Here is an example for the french alphabet that could help you for a custom sort:

var alpha = function(alphabet, dir, caseSensitive){
  return function(a, b){
    var pos = 0,
      min = Math.min(a.length, b.length);
    dir = dir || 1;
    caseSensitive = caseSensitive || false;
    if(!caseSensitive){
      a = a.toLowerCase();
      b = b.toLowerCase();
    }
    while(a.charAt(pos) === b.charAt(pos) && pos < min){ pos++; }
    return alphabet.indexOf(a.charAt(pos)) > alphabet.indexOf(b.charAt(pos)) ?
      dir:-dir;
  };
};

To use it on an array of strings a:

a.sort(
  alpha('ABCDEFGHIJKLMNOPQRSTUVWXYZaàâäbcçdeéèêëfghiïîjklmnñoôöpqrstuûüvwxyÿz')
);

Add 1 or -1 as the second parameter of alpha() to sort ascending or descending.
Add true as the 3rd parameter to sort case sensitive.

You may need to add numbers and special chars to the alphabet list

Mic
  • 24,812
  • 9
  • 57
  • 70
  • If you are using this code, also see: http://stackoverflow.com/questions/3630645/how-to-compare-utf-8-strings-in-javascript/3633725#3633725 – Tomasz Wysocki Sep 04 '10 at 12:56
  • Eek! Do you really have to go through all that? What about forgetting to put it into Normalization Form D first? Does PHP truly have nothing equivalent to Perl’s [Unicode::Collate](http://search.cpan.org/perldoc?Unicode%3A%3ACollate) and [Unicode::Collate::Locale](http://search.cpan.org/perldoc?Unicode%3A%3ACollate%3A%3ALocale) modules? **REALLY?** It seems like utter madness to try to reïmplement all that on one’s own! – tchrist Nov 19 '10 at 04:25
  • 2
    @tchrist, it is not PHP, but javascript here, and it is like it is. – Mic Nov 20 '10 at 20:53
13

You may be able to build your own sorting function using localeCompare() that - at least according to the MDC article on the topic - should sort things correctly.

If that doesn't work out, here is an interesting SO question where the OP employs string replacement to build a "brute-force" sorting mechanism.

Also in that question, the OP shows how to build a custom textExtract function for the jQuery tablesorter plugin that does locale-aware sorting - maybe also worth a look.

Edit: As a totally far-out idea - I have no idea whether this is feasible at all, especially because of performance concerns - if you are working with PHP/mySQL on the back-end anyway, I would like to mention the possibility of sending an Ajax query to a mySQL instance to have it sorted there. mySQL is great at sorting locale aware data, because you can force sorting operations into a specific collation using e.g. ORDER BY xyz COLLATE utf8_polish_ci, COLLATE utf8_german_ci.... those collations would take care of all sorting woes at once.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Thx. for links. It's little shame that JavaScript doesn't support it in core, but still it's working solution. – Tomasz Wysocki Sep 02 '10 at 20:13
  • 1
    Be careful with `localeCompare()` in IE6: http://blog.schmichael.com/2008/07/14/javascript-collation-fail/ – BalusC Sep 02 '10 at 20:14
  • @BalusC the comments in that article claim that it's in fact Wine's fault, not IE6's. Can't find anything else on the issue to confirm or disprove it, and I'm too lazy to build a test case right now... @Tomasz if you go this route, it would be interesting to hear whether things work well in IE6. – Pekka Sep 02 '10 at 20:44
  • Oh, I didn't see the comment before. In any way, to avoid unforeseen browser inconsistenties (I still don't have a solid feeling around `localeCompare()`), I'd implement a custom one like Tomalak did in your linked topic. – BalusC Sep 02 '10 at 21:20
  • @BalusC I agree that's probably the best and most solid way to go. – Pekka Sep 02 '10 at 21:39
  • From "hard-code" approaches I most like Mic's one. Its more explicit than replace-compare one. localeCompare would be great but don't work for all browser/configurations (ie. don't work for my Google Chrome, but works fine on Fifrefox (same computer)). – Tomasz Wysocki Sep 03 '10 at 07:27
9

Mic's code improved for non-mentioned chars:

var alpha = function(alphabet, dir, caseSensitive){
  dir = dir || 1;
  function compareLetters(a, b) {
    var ia = alphabet.indexOf(a);
    var ib = alphabet.indexOf(b);
    if(ia === -1 || ib === -1) {
      if(ib !== -1)
        return a > 'a';
      if(ia !== -1)
        return 'a' > b;
      return a > b;
    }
    return ia > ib;
  }
  return function(a, b){
    var pos = 0;
    var min = Math.min(a.length, b.length);
    caseSensitive = caseSensitive || false;
    if(!caseSensitive){
      a = a.toLowerCase();
      b = b.toLowerCase();
    }
    while(a.charAt(pos) === b.charAt(pos) && pos < min){ pos++; }
    return compareLetters(a.charAt(pos), b.charAt(pos)) ? dir:-dir;
  };
};

function assert(bCondition, sErrorMessage) {
      if (!bCondition) {
          throw new Error(sErrorMessage);
      }
}

assert(alpha("bac")("a", "b") === 1, "b is first than a");
assert(alpha("abc")("ac", "a") === 1, "shorter string is first than longer string");
assert(alpha("abc")("1abc", "0abc") === 1, "non-mentioned chars are compared as normal");
assert(alpha("abc")("0abc", "1abc") === -1, "non-mentioned chars are compared as normal [2]");
assert(alpha("abc")("0abc", "bbc") === -1, "non-mentioned chars are compared with mentioned chars in special way");
assert(alpha("abc")("zabc", "abc") === 1, "non-mentioned chars are compared with mentioned chars in special way [2]");
Tomasz Wysocki
  • 11,170
  • 6
  • 47
  • 62
0

You have to keep two sortkey strings. One is for primary order, where German ä=a (primary a->a) and French é=e (primary sortkey e->e) and one for secondary order, where ä comes after a (translating a->azzzz in secondary key) or é comes after e (secondary key e->ezzzz). Especially in Czech some letters are variations of a letter (áéí…) whereas others stand in their full right in the list (ABCČD…GHChI…RŘSŠT…). Plus the problem to consider digraphs a single letters (primary ch->hzzzz). No trivial problem, and there should be a solution within JS.

xandru
  • 1
-1

Funny, I have to think about that problem and finished searching here, because it came in mind, that I can use my own javascript module. I wrote a module to generate a clean URL, therefor I have to translitate the input string... (http://pid.github.io/speakingurl/)

var mySlug = require('speakingurl').createSlug({
    maintainCase: true,
    separator: " "
});

var input = "Schöner Titel läßt grüßen!? Bel été !";
var result;

slug = mySlug(input);
console.log(result); // Output: "Schoener Titel laesst gruessen bel ete"

Now you can sort with this results. You can ex. store the original titel in the field "title" and the field for sorting in "title_sort" with the result of mySlug.

pid
  • 744
  • 7
  • 3