Sorting non ascii characters in Dart

168 Views Asked by At

Users submitted those weird characters. How to sort them along with normal text in Dart ?

The good old b.name.toLowerCase().compareTo(a.name.toLowerCase()); does not work

1

There are 1 best solutions below

0
JaffaKetchup On

UPDATE: Just found https://pub.dev/packages/diacritic, which appears as though it might do roughly what you want, although I'm not sure of how many different characters it supports.


Well, a good place to start might be looking at the internal implementation of the ES6 String.prototype.normalize() function.

Interestingly, it only works in Compatibility Decomposition mode - ie. NFKC or NFKD.

for (const weirdString of [
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  'ℝ',
]) {console.log(weirdString.normalize('NFKC'))}


Anyway, maybe there is a translation in Dart, or you can use the 'dart:js' library if you're running on the web, or you could find the implementation in the V8 engine (although it'll probably be in C).