Issue
I have an array:
[
[0, 'Please select a Customer'],
[2072, 'S-Customer'],
[834, '01-Customer'],
[709, 'C-Customer'],
[4217, 'Test'],
[2074, 'A-Customer']
]
but how can I sort it numeric and alphabetically and still have the same ID which is the first value?? It would be like this:
obs: the first value should not be changed.
[
[0, 'Please select a Customer'],
[834, '01-Customer'],
[2074, 'A-Customer'],
[709, 'C-Customer'],
[2072, 'S-Customer'],
[4217, 'Test']
]
Solution
You could keep the first array in place by looking to index 0 and sort the rest by the value of index 1
const
array = [[0, 'Please select a Customer'], [2072, 'S-Customer'], [834, '01-Customer'], [709, 'C-Customer'], [4217, 'Test'], [2074, 'A-Customer']];
array.sort((a, b) => !b[0] - !a[0] || a[1].localeCompare(b[1]));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answered By - Nina Scholz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.