Hmm, it looks like your compareFullUnicode
exacly the same as (a > b) - (a < b)
. Could you give me an example of when they have different sorting results?
const arr = [
"A", "B", "C", "Č", "Ć", "D",
"Dž", "Đ", "E", "F", "G", "H",
"I", "J", "K", "L", "Lj", "M",
"N", "Nj", "O", "P", "R", "S",
"ÛŒ", "T", "U", "ℍ",
// has surrgogate pairs
"🄰", "🄲", "🄱"
];
console.log("naive sort:");
console.log(arr.slice(0).sort((a, b) => (a > b) - (a < b)));
console.log("");
console.log("unicode-aware sort:");
console.log(arr.slice(0).sort(compareFullUnicode));
console.log("");
console.log("localeCompare sort:");
console.log(arr.slice(0).sort((a, b) => a.localeCompare(b)));
console.log("");
console.log("Intl.Collator sort:");
console.log(arr.slice(0).sort(new Intl.Collator('en').compare));
console.log("");
Output:
naive sort:
[
'A', 'B', 'C', 'D', 'Dž', 'E',
'F', 'G', 'H', 'I', 'J', 'K',
'L', 'Lj', 'M', 'N', 'Nj', 'O',
'P', 'R', 'S', 'T', 'U', 'ی',
'Ć', 'Č', 'Đ', 'ℍ', '🄰', '🄱',
'🄲'
]
unicode-aware sort:
[
'A', 'B', 'C', 'D', 'Dž', 'E',
'F', 'G', 'H', 'I', 'J', 'K',
'L', 'Lj', 'M', 'N', 'Nj', 'O',
'P', 'R', 'S', 'T', 'U', 'ی',
'Ć', 'Č', 'Đ', 'ℍ', '🄰', '🄱',
'🄲'
]
localeCompare sort:
[
'A', '🄰', 'B', '🄱', 'C', '🄲',
'Ć', 'Č', 'D', 'Đ', 'Dž', 'E',
'F', 'G', 'H', 'ℍ', 'I', 'J',
'K', 'L', 'Lj', 'M', 'N', 'Nj',
'O', 'P', 'R', 'S', 'T', 'U',
'ی'
]
Intl.Collator sort:
[
'A', '🄰', 'B', '🄱', 'C', '🄲',
'Ć', 'Č', 'D', 'Đ', 'Dž', 'E',
'F', 'G', 'H', 'ℍ', 'I', 'J',
'K', 'L', 'Lj', 'M', 'N', 'Nj',
'O', 'P', 'R', 'S', 'T', 'U',
'ی'
]