Is there a more professional way to sort people alphabetically by last name in an array?
Here is the array I'm working with:
const people = [ 'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig', 'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter' ];
Although my code is functioning correctly, I'm puzzled by the unexpected results of the split method. Even after seeking help from Poe, I am still unsure.
When Poe assisted me, the positions of the destructured variables were changed, but I have not yet modified the code.
const alpha = people.sort((a, b) => {
const [aFirst, aLast] = a.split(', '); // expected ["Bernhard", "Sandra"] Not ['Sandra', 'Bernhard']
const [bFirst, bLast] = b.split(', '); // expected ["Bethea", "Erin"] Not ['Erin', 'Bethea']
return aFirst > bFirst ? 1 : -1;
})
console.table(alpha);