Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

functional non-destructive array sort

Apart for the native way of cloning an array and then sorting it in place, is there an algorithm and existing implementation that is more suited for non-destructive sorting?

Need to sort an array of floats into a new array without changing the source. My search results were rather thin since most of the literature is focused on reducing the memory requirements with in-place sorting.

Using the native sorted = [].slice().sort() works fine. This question is about understanding if there are other performant sorting implementations when memory constraints are removed since a new array is needed anyway.

like image 705
Hurelu Avatar asked May 25 '15 04:05

Hurelu


1 Answers

There's a simpler syntax for immutably sorting an array using ES6 spread operator:

[...array].sort(sortFn) 
like image 149
shuaibird Avatar answered Sep 21 '22 00:09

shuaibird