-
-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort with ignore case #347
Comments
Hi, |
The current implementation is not even "case-sensitive", it is just Unicode code point sorting. Both case-sensitive and -insensitive are language-dependent, so I recommend using const list = ["SASU", "säsä", "SISU", "sisi", "TATA", "ZÖZU", "zyzy"];
list.toSorted((a, b) => a > b ? 1 : -1); // current
// => ["SASU","SISU","TATA","ZÖZU","sisi","säsä","zyzy"]
list.toSorted((a, b) => a.toLowerCase() > b.toLowerCase() ? 1 : -1); // simple lowercase
// => ["SASU","sisi","SISU","säsä","TATA","zyzy","ZÖZU"]
const englishName = new Intl.DisplayNames(["en"], {type: "language"});
Object.fromEntries(["en", "fr", "sv", "et", "tr"].map((ln) => [englishName.of(ln), list.toSorted((a, b) => a.localeCompare(b, ln))]));
// {
// "English": ["säsä","SASU","sisi","SISU","TATA","ZÖZU","zyzy"],
// "French": ["säsä","SASU","sisi","SISU","TATA","ZÖZU","zyzy"],
// "Swedish": ["SASU","sisi","SISU","säsä","TATA","zyzy","ZÖZU"],
// "Estonian": ["SASU","sisi","SISU","säsä","ZÖZU","zyzy","TATA"],
// "Turkish": ["säsä","SASU","SISU","sisi","TATA","ZÖZU","zyzy"]
// } |
Thx for the suggestion 👍🏻 |
Hi, thank you for your work, but I am a little confused when I just checked the implementation. Do you want, for example, |
@yheuhtozr thx for your attention. I just pushed another version of the sort function: The original implementation is back but with accented characters treated as non-accented characters. It's not locale specific sorting but it's better than having them listed after non-accented uppercase items. The case-insensitive sorting remains untouched, it uses |
@Bubka Sorry for being away, and being confused in conversation myself, but there look like a lot of concepts having been mixed up under the name of "case sensitivity", so I am not exactly sure what is the goal you actually intends for. Let's call them by different names:
So, we have tons of possible options, but first, we were not clear which "case-sensitive" means, case-unmerged or case-separated as I named.
And I'm not sure what you tried to do with "case-sensitive" branches in 5d3a1be and d90ffd5.
Generally, I don't know much what aspect and what degree of "case-sensitive" behavior of old implementation you are thinking to retain. Do you want to mix old behavior and locale support in any combination? Do you actually want to have more than two sorting options? Do you perhaps need some help in creating other "case-sensitive" version using |
Thx very much @yheuhtozr for the axis decomposition, it's very useful. And I must admit, I hadn't thought of all those combinations 🤯 What I was aiming for with the case-sensitive branch is case-separated + variant-merged + local_dependant. I haven't found a way to achieve this with a single Any help would be appreciated. |
Yes..., in a bigger picture, there is no idiomatic way to write case-separated sort in general (you can only loop them manually). Which means you handle some part of low-level Unicode works yourself when you want it. So a sample code that satisfies your description would be like: let locale; // <- assume this is the current display language
let namesToSort; // <- assume this is an array that contains names
// sorter for reuse
// maybe you can put it inside the function if the sort only perform once every page
let caseSensitiveSorter = new Intl.Collator(locale, {
sensitivity: "case", // <- focus on case comparison
caseFirst: "upper", // <- make sure 'A' > 'a' (not 'a' > 'A')
});
// locale-dependent segmenter
// maybe you can put it inside the function
// Firefox only supports at >= 125 so you can fall back to simple [...string] if with any concern
let segmenter = new Intl.Segmenter(locale, {granularity: "grapheme"});
// sort function
// I don't know what is optimal but it returns a new sorted list based on input list
function caseSensitiveSort(list, sorter, segmenter) {
let segList = list.map((e) => [...segmenter.segment(e)].map((s) => s.segment));
segList.sort(function(a, b) {
for (let i = 0; true; i++) {
const result = sorter.compare(a[i] || '', b[i] || '');
if (result !== 0 || !a[i] || !b[i]) { return result }
}
});
return segList.map((e) => e.join(''));
}
let sortedNames = caseSensitiveSort(namesToSort, caseSensitiveSorter, segmenter); // execute sorting |
Is your feature request related to a problem? Please describe.
I would like to be able to sort from A-Z or Z-A but with ignore case. I have entries starting with UPPER and lower case, so (for me) it would be nice to ignore it during sort.
Describe the solution you'd like
A checkbox to ignore the case during sort right next to A-Z and Z-A.
The text was updated successfully, but these errors were encountered: