Sorting query results with German umlaut

Viewed 362

is there a chance to sort results of a cypher query including german umlaut like ä,ö,ü? At the moment I get a list alphabetical sorted and nodes starting with an umlaut are put at the end of the list. Normally they should be within the list e.g. an 'Ö' should be equal to 'OE'.

Any ideas are appreciated, thanks.

2 Answers

Since you asked specifically about Cypher, the query below is an example of how to sort umlauted characters as if they were their ligatured equivalents (e.g., treating 'Ü' as if it was 'UE').

WITH ['Dorfer', 'Dörfener'] AS names
UNWIND names AS name
RETURN name
ORDER BY
  REDUCE(s = name, x IN [
    ['ä', 'ae'], ['Ä', 'AE'],
    ['ö', 'oe'], ['Ö', 'OE'],
    ['ü', 'ue'], ['Ü', 'UE']] |
    REPLACE(s, x[0], x[1]));

The above query will return 'Dörfener' first, and 'Dorfer' second.

However, the above approach is not very efficient, since it calls the REPLACE function six times for each name. It would be much more efficient to write a user-defined procedure in Java that took the entire names list as input and returned the sorted list in a single call.

Yes, you can use localeCompare or Intl.Collator to achieve this:

// Our original array
// Outputs ["u", "x", "ü", "ü", "ü"]
const input1 = ['ü','u','ü','x','ü'];
const output1 = input1.sort();
console.log(output1);

// Intl.Collator
// Outputs ["u", "ü", "ü", "ü", "x"]
const input2 = ['ü','u','ü','x','ü'];
const output2 = input2.sort(Intl.Collator().compare);
console.log(output2)

// localeCompare
// Outputs ["u", "ü", "ü", "ü", "x"]
const input3 = ['ü','u','ü','x','ü'];
const output3 = input3.sort(function (a, b) {
    return a.localeCompare(b);
});
console.log(output3)

Related