Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When sorting strings should é come before e

If I am sorting two strings café and cafe is there a best practice to follow as to which letter comes first? I tested localeCompare in javascript and café comes before cafe but I don't understand why.

like image 499
Chris.Stover Avatar asked Jul 03 '13 17:07

Chris.Stover


People also ask

How do you sort by strings?

We have two ways to sort strings such as Using sort() method or Using Loop. On using sort() method, we are using a predefined method by JavaScript to sort array of strings. Sort() method is applicable only for alphabetic strings. Sort() method does not support array of numbers.

What does sort do for strings?

The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

How do you sort a string in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.

How does sort function work in JavaScript?

When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. If the result is negative, a is sorted before b . If the result is positive, b is sorted before a .


1 Answers

Best practice is to sort without diacritics first, ie. cafe comes before café.

localeCompare works by stripping the diacritics, so the sort order doesn't reflect the real words, since café is turned into cafe

You can read more about localeCompare here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

like image 81
Martin Jespersen Avatar answered Oct 23 '22 22:10

Martin Jespersen