serv: javascript to search ignoring accents
HT wants Blüh to sort Blue Blüh Blum Blz rather than the default Blue Blum Blz Blüh
Here's the original code to sort the json by name using just the raw unicode
thisCatFeatures.sort(function(a, b) {
var aName = a.getProperties().name.toUpperCase();
var bName = b.getProperties().name.toUpperCase();
if (aName < bName) { return -1; }
if (aName > bName) { return 1; }
return 0;
});
Here's a modification I made which transforms each accented character to its base character (ä to a) by splitting the base from the combining diacritic and then stripping the diacritic. This addresses the immediate problem, but does not conform with e.g. the German convention which requires that ä actually sort as ae.
thisCatFeatures.sort(function(a, b) {
var aName = a.getProperties().name.toUpperCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
var bName = b.getProperties().name.toUpperCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
if (aName < bName) { return -1; }
if (aName > bName) { return 1; }
return 0;
});
Here's a variation using the Collator object with an argument telling it to use German (de) spelling conventions. The "{sensitivity: base}" argument in combination with the German localization might be redundant or might generate slightly different results than no such argument, I haven't tested and this makes it explicit.
thisCatFeatures.sort(function(a, b) {
var aName = a.getProperties().name.toUpperCase();
var bName = b.getProperties().name.toUpperCase();
return new Intl.Collator('de', { sensitivity: 'base' }).compare(aName,bName);
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator