You're right that if two elements have the same identifier, the order doesn't matter for those particular elements. In many sorting algorithms, including JavaScript's native .sort(), elements that compare as equal remain in their original order (stable sort). So, your simplified version is equally effective:
This will work just fine even with repeated identifiers.
2. The splice Example and Array Mutation
I apologize for the misunderstanding. You're correct; your example using splice actually doesn't mutate the original array because you made a shallow copy (a=[...arr]) before applying the splice.
In this function, a=[...arr] creates a new array that is a copy of the original arr. Then, splice is called on this new array (a), so the original arr remains unchanged. Therefore, it is a non-mutating operation on the original array.
Thank you for your keen observations; they serve to improve the quality and accuracy of the discussion.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
1. The
sortByFunction with Repeated IdentifiersYou're right that if two elements have the same identifier, the order doesn't matter for those particular elements. In many sorting algorithms, including JavaScript's native
.sort(), elements that compare as equal remain in their original order (stable sort). So, your simplified version is equally effective:This will work just fine even with repeated identifiers.
2. The
spliceExample and Array MutationI apologize for the misunderstanding. You're correct; your example using
spliceactually doesn't mutate the original array because you made a shallow copy (a=[...arr]) before applying thesplice.Here's the function for clarity:
In this function,
a=[...arr]creates a new array that is a copy of the originalarr. Then,spliceis called on this new array (a), so the originalarrremains unchanged. Therefore, it is a non-mutating operation on the original array.Thank you for your keen observations; they serve to improve the quality and accuracy of the discussion.