1

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']
]
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
João Marcos
  • 63
  • 1
  • 6

1 Answers1

1

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392