0

I'm trying to sort a list of strings with accented characters e.g.["Zebra", "Apple", "Ähhhh"] and I want it to look like ["Apple", "Ähhhh", "Zebra"]

Just running list.sort() gives me ["Apple","Zebra","Ähhhh"]

Is there another built in function (like localeCompare) that will let me sort this in the way I want it to be?

Thanks!

Tim
  • 2,221
  • 6
  • 22
  • 43
  • So you want `Ä`s to come after `A`s, is that the logic you're looking for? – CertainPerformance Nov 07 '18 at 20:26
  • What locale do you want to use, so that `Ä` comes after `A`? – syntagma Nov 07 '18 at 20:26
  • By the way, you're question may already have an answer at https://stackoverflow.com/questions/12624532/locale-based-sort-in-javascript-sort-accented-letters-and-other-variants-in-a-p, though your's is much more succinctly worded. – pwilcox Nov 07 '18 at 20:35

1 Answers1

3

In general, yes, you can sort by different locale.

let ar = ["Apple","Zebra","Ähhhh"];

ar.sort((a,b) => a.localeCompare(b, 'en'))

However, as to the exact sorting you're looking for, you'll have to replace 'en' with the appropriate locale, if there is one.

pwilcox
  • 5,542
  • 1
  • 19
  • 31
  • This results in `"Ähhhh", "Apple", "Zebra"`, which isn't what OP is looking for – CertainPerformance Nov 07 '18 at 20:32
  • @CertainPerformance, hence my last sentence. I still put it as an answer because I suspect that he's really looking for accented a's and non-accented a's to be together, and would respect the locale if it it does it differently than what he asked for. – pwilcox Nov 07 '18 at 20:34