0

How to sort object by its value alphabetical order not by key? Here is object

{
1: "Arabic"
2: "Bulgarian"
3: "Catalan"
4: "Chinese"
5: "Croatian"
6: "Czech"
7: "Danish"
8: "Dutch"
9: "English"
10: "Estonian"
11: "Finnish"
12: "French"
13: "German"
14: "Greek"
15: "Hebrew"
16: "Hungarian"
17: "Icelandic"
18: "Indonesian"
19: "Italian"
20: "Japanese"
21: "Korean"
22: "Latvian"
23: "Lithuanian"
24: "Norwegian"
25: "Polish"
26: "Portuguese"
27: "Romanian"
28: "Russian"
29: "Serbian"
30: "Slovak"
31: "Slovenian"
32: "Spanish"
33: "Swedish"
34: "Turkish"
35: "Afrikaans"
}

and desired output by alphabetical order

{ 35: "Afrikaans" 1: "Arabic" 2: "Bulgarian" 3: "Catalan" 4: "Chinese" 5: "Croatian" 6: "Czech" 7: "Danish" 8: "Dutch" 9: "English" 10: "Estonian" 11: "Finnish" 12: "French" 13: "German" 14: "Greek" 15: "Hebrew" 16: "Hungarian" 17: "Icelandic" 18: "Indonesian" 19: "Italian" 20: "Japanese" 21: "Korean" 22: "Latvian" 23: "Lithuanian" 24: "Norwegian" 25: "Polish" 26: "Portuguese" 27: "Romanian" 28: "Russian" 29: "Serbian" 30: "Slovak" 31: "Slovenian" 32: "Spanish" 33: "Swedish" 34: "Turkish" }

Ashish Rai
  • 11
  • 3
  • There's no array here, just an object. What's the expected output? – Mureinik Jul 01 '21 at 08:32
  • My expected output is {35: "Afrikaans",1: "Arabic" 2: "Bulgarian" 3: "Catalan" 4: "Chinese" 5: "Croatian" 6: "Czech" 7: "Danish" 8: "Dutch" 9: "English" 10: "Estonian" 11: "Finnish" 12: "French" 13: "German" 14: "Greek" 15: "Hebrew" 16: "Hungarian" 17: "Icelandic" 18: "Indonesian" 19: "Italian" 20: "Japanese" 21: "Korean" 22: "Latvian" 23: "Lithuanian" 24: "Norwegian" 25: "Polish" 26: "Portuguese" 27: "Romanian" 28: "Russian" 29: "Serbian" 30: "Slovak" 31: "Slovenian" 32: "Spanish" 33: "Swedish" 34: "Turkish"} – Ashish Rai Jul 01 '21 at 08:33
  • Could you update your expected result on your question? – Pylon Jul 01 '21 at 08:37
  • You can't really sort object keys. Perhaps you meant to create an array? – Mureinik Jul 01 '21 at 08:45
  • I would like to know the use case. – im_tsm Jul 01 '21 at 08:47

2 Answers2

0

welcome to stackOverflow, this question would be on Javascript section.

const obj = {1: "Arabic", ... }

For your question, if you need an Array sorted:

const sorted = Object.values(obj).sort();

If you need an object, with sorted array:

const newObj = {}
sorted.forEach((name, index) => newObj[index] = name)
Adrián
  • 175
  • 9
0

Actually, it's an object instead of an array of your given input. We could not sort on an object but we could determine the iteration order over an object by overriding the Iterator interface as below:

const myObj = {
  1: "Arabic",
  2: "Bulgarian",
  3: "Catalan",
  4: "Chinese",
  5: "Croatian",
  6: "Czech",
  7: "Danish",
  8: "Dutch",
  9: "English",
  10: "Estonian",
  11: "Finnish",
  12: "French",
  13: "German",
  14: "Greek",
  15: "Hebrew",
  16: "Hungarian",
  17: "Icelandic",
  18: "Indonesian",
  19: "Italian",
  20: "Japanese",
  21: "Korean",
  22: "Latvian",
  23: "Lithuanian",
  24: "Norwegian",
  25: "Polish",
  26: "Portuguese",
  27: "Romanian",
  28: "Russian",
  29: "Serbian",
  30: "Slovak",
  31: "Slovenian",
  32: "Spanish",
  33: "Swedish",
  34: "Turkish",
  35: "Afrikaans",
};

myObj[Symbol.iterator] = function* () {
  const entries = Object.entries(this).sort((a, b) => a[1].localeCompare(b[1]));

  for (const [key, value] of entries) {
    // you could determine what structure looks like here
    yield { [key]: value };
  }
};

for (const x of myObj) {
  console.log(x);
}
Pylon
  • 698
  • 6
  • 9