Friday, November 1, 2019

javascript - Apply array.sort() default algorithm to string properties of objects in an array

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.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...