Friday 1 November 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...