0

From Array.prototype.sort() documentation:

The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

What if target array's elements are objects and we want apply above algorithm to targetKey's value?

const targetArray = [
    { targetKey: 'March' },
    { targetKey: 'Jan' },
    { targetKey: 'Feb' },
    { targetKey: 'Dec' }
]

Expected result:

[
    { targetKey: 'Dec' },
    { targetKey: 'Feb' },
    { targetKey: 'Jan' },
    { targetKey: 'March' }
]

Some simple solutions?

Warning: We don't considering the ordering by alphabet in this question. We need to order due sequences of UTF-16 code units values.

adiga
  • 34,372
  • 9
  • 61
  • 83
Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124
  • 2
    Have you tried something or just looking for someone to write it for you? – Kosh Feb 01 '19 at 05:00
  • @KoshVery, No. Currently, I need to know, are some simple solutions exists or no. If no - I'll find the complicated solution. – Takeshi Tokugawa YD Feb 01 '19 at 05:02
  • 4
    Possible duplicate of [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) and [How to sort object array based on key in typescript](https://stackoverflow.com/questions/39850339) and [Sort Array of Objects by specific key value](https://stackoverflow.com/questions/43816557) and [Sort Array of object by object field](https://stackoverflow.com/questions/51194830) – adiga Feb 01 '19 at 07:20

3 Answers3

2

You can use the compareFunction function with String::localeCompare() on the target keys:

const targetArray = [
    { targetKey: 'March' },
    { targetKey: 'Jan' },
    { targetKey: 'Feb' },
    { targetKey: 'Dec' }
];

targetArray.sort((a, b) => a.targetKey.localeCompare(b.targetKey));

console.log(targetArray)

And if you don't want to mutate (modify) the original array, you can clone it with slice() and then sort the cloned one.

const targetArray = [
    { targetKey: 'March' },
    { targetKey: 'Jan' },
    { targetKey: 'Feb' },
    { targetKey: 'Dec' }
];

let res = targetArray.slice(0).sort((a, b) => a.targetKey.localeCompare(b.targetKey));

console.log(targetArray)
console.log(res);
Shidersz
  • 16,846
  • 2
  • 23
  • 48
2

You can also use destructing assingment to get the targetKey and then use .localeCompare to compare both values:

const targetArray = [
    { targetKey: 'March' },
    { targetKey: 'Jan' },
    { targetKey: 'Feb' },
    { targetKey: 'Dec' }
],

res = targetArray.sort(({targetKey:a}, {targetKey:b}) => a.localeCompare(b));
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

You can use Intl.collator

const targetArray = [
    { targetKey: 'March' },
    { targetKey: 'Jan' },
    { targetKey: 'Feb' },
    { targetKey: 'Dec' }
];

let op = targetArray.sort(({targetKey:a},{targetKey:b})=>new Intl.Collator('en').compare(a,b));

console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60