I have the following code snippet:
class MyClass {
constructor(name) {
this.name = name;
}
toString() {
return this.name;
}
valueOf() {
return this.name;
}
}
const list = [new MyClass("b"), new MyClass("ä")];
list.sort();
console.log(list.join());
list.sort(new Intl.Collator('de').compare);
console.log(list.join());
In this scenario, using the Collator
is necessary to achieve the correct sorting order. However, I am interested in eliminating this requirement and being able to compare instances of MyClass
simply with something like myClass1 < myClass2
. This could potentially be achieved by modifying the valueOf
function.
So my query is, are there any existing JavaScript functionalities that allow me to obtain a "locale" value from a string (similar to Intl.Collator
or localeCompare
) which I can then return in my valueOf
function to correctly determine the ordering?