8

I have an array of characters I want to sort:

const arr = ['z', 'a', 'Z', 'A'];

I want the sorted order to be: upper-case characters alphabetically, followed by lower-case characters alphabetically:

['A', 'Z', 'a', 'z']

This is trivial to accomplish using .sort() without any parameters:

const arr = ['z', 'a', 'Z', 'A'];
arr.sort();
console.log(arr);

But how could localeCompare be used to determine the same relative position of each character? The caseFirst option looked promising:

Whether upper case or lower case should sort first. Possible values are "upper", "lower", or "false" (use the locale's default); the default is "false". This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.

But it doesn't appear to do what I want. I've tried many variants:

// Desired result: ['A', 'Z', 'a', 'z']

const arr = ['z', 'a', 'Z', 'A'];
arr.sort((a, b) => a.localeCompare(b));
console.log(arr);
arr.sort((a, b) => a.localeCompare(b, undefined, { caseFirst: 'upper' }));
console.log(arr);
arr.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'case', caseFirst: 'upper' }));
console.log(arr);
arr.sort((a, b) => a.localeCompare(b, 'en', { caseFirst: 'upper' }));
console.log(arr);
arr.sort((a, b) => a.localeCompare(b, 'kf', { caseFirst: 'upper' }));
console.log(arr);

I'd like it to perform similarly to .sort() (at least for alphabetical characters), which compares each character's code point:

['A', 'Z', 'a', 'z'].forEach((char) => {
  console.log(char.charCodeAt());
});

Of course, I know I can simply avoid localeCompare and use .sort() without a callback, or use the charCodeAt method above:

const arr = ['z', 'a', 'Z', 'A'];
arr.sort((a, b) => a.charCodeAt() - b.charCodeAt());
console.log(arr);

But how would I do it using localeCompare?

Snow
  • 3,820
  • 3
  • 13
  • 39
  • I will reward a bounty to a good answer. – Snow Mar 04 '19 at 01:42
  • 2
    There is no _good_ answer as your very well written question demonstrates. The only answer is to filter into two arrays (upper,lower), sort each using `localeCompare()` then `.merge()` then back together. – Randy Casburn Mar 04 '19 at 01:48
  • Would have been useful to know which features from `localeCompare` you wanted. – Kaiido Jan 09 '23 at 08:48
  • @Kaiido Whichever features that can compare lexicographically, at least for plain alphabetical characters. I don't know what those features are or if they exist - I suspect they don't - but if they do, I'm not picky. I can easily accomplish it without `localeCompare` but was just curious if it's something `localeCompare` could somehow be used for too. It looks much nicer than the conditional operator, and `.sort()` without arguments only works when sorting arrays of strings - not for objects, and not when outside the context of array sorting. – Snow Jan 09 '23 at 21:01
  • So... you want to use `localeCompare` just because it *looks* good? You don't actually need anything it does like considering `é` before `f`? – Kaiido Jan 10 '23 at 08:32

1 Answers1

-2

Was able to sort your array lexicographically using this.

const lexicalCompare = (a, b) =>
  a < b ? -1 : (a > b ? 1 : 0);

const arr = ['z', 'a', 'Z', 'A'];

console.log(arr.sort(lexicalCompare));
joboyx
  • 35
  • 4
  • 2
    That's the same as no sorter OP said they didn't want. They want to use `localeCompare` (for I don't know what reasons) – Kaiido Jan 09 '23 at 08:48