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
Allow specifying locale in lexicographicSortSchema
#2869
Comments
@vhenzl Thanks for taking the time to open this issue. |
Hi @IvanGoncharov! First of all, sorry for the late answer. And thanks for looking into it. You are probably right, supporting locales is likely overkill. Using a simple character comparison would for sure work for my use case. My objective here is to get a stable order regardless of system locale, but not necessarily alphabetically/lexicographically correct order. Which I guess should suit most of the Since the alphabet is restricted to English letters, only digraphs (trigraphs?) and letter case are interesting in terms of sorting. There is a handful of languages that sort digraphs in special ways, most other languages threat them as combinations of separate letters. With a simple character comparison, words with digraphs would be placed differently than they should be in the given language. However, for someone to be really affected by that, it would mean that names in the sorted GraphQL schema are in that non-English language (who doesn't use English for programming?) and that the order really matters for the given use case (maybe something like a table of contents). So to cut a long story short, the chances that it would affect someone negatively are IMO minimal. I'm not so sure about different order for letter cases tho, because it would affect all languages: function ascii(x, y) {
if(x > y) return 1;
if(x < y) return -1;
return 0
}
function asciiUpperCase(xx, yy) {
const x = xx.toUpperCase();
const y = yy.toUpperCase();
if(x > y) return 1;
if(x < y) return -1;
return 0
}
const a = ['c', 'b', 'a', 'Ab', 'ab', 'AB', 'aB', '10', '5', 'C', 'B', 'A'];
[...a].sort(asciiUpperCase)
// ["10", "5", "a", "A", "Ab", "ab", "AB", "aB", "b", "B", "c", "C"]
[...a].sort(ascii) // same as [...a].sort()
// ["10", "5", "A", "AB", "Ab", "B", "C", "a", "aB", "ab", "b", "c"]
[...a].sort((x, y) => x.localeCompare(y, 'cs')) // same as 'en' locale
// ["10", "5", "a", "A", "ab", "aB", "Ab", "AB", "b", "B", "c", "C"] So names using camelCase would be sorted differently then they are now. As for the #2876 PR, I'm not sure I understand what is function sortBy<T>(
array: $ReadOnlyArray<T>,
mapToKey: (T) => string,
): Array<T> {
return array.slice().sort((obj1, obj2) => {
const key1 = mapToKey(obj1).toUpperCase();
const key2 = mapToKey(obj2).toUpperCase();
if (key1 > key2) {
return 1;
}
if (key1 < key2) {
return -1;
}
return 0;
});
} I tried the change locally on my project and it works just fine. Using |
I use
lexicographicSortSchema()
to get a stable output when generating TS types from GraphQL schema and recently I discovered a problem when a field calledchemicalName
is misplaced and causing a diff.The reason is that one of my machines has non-English, Czech locale and in Czech alphabet, the digraph Ch is handled as a single letter and sorted between H and I.
Example
Would be great if
lexicographicSortSchema()
could acceptlocales
and possibly alsooptions
parameters forlocaleCompare
to be able to force a particular (English) locale for the schema sorting independently on the machine's locale.The text was updated successfully, but these errors were encountered: