功能性无损阵列排序

除了克隆一个数组然后就地排序的本机方法之外,是否有一种算法和现有的实现更适合于非破坏性排序?

需要在不更改源的情况下将浮点数组排序到新数组中。我的搜索结果相当薄,因为大多数文献都关注于通过就地排序来减少内存需求。

使用本机 sorted = [].slice().sort()工作得很好 。这个问题是关于理解在删除内存约束时是否存在其他性能排序实现,因为无论如何都需要一个新数组。

36477 次浏览

As the comments have repeated a few times:

  1. shuffledArray.slice().sort() is the default way to go.
  2. It's not really clear how we could have a better algorithm / method using the libraries your mentioned.

Seeing as the motivation for non-destructive sorting is related to writing functional code, and you're looking at Ramda...check out Facebook's ImmutableJS library if you haven't already.

Particularly, the Seq. You could start storing your array of floats in a Seq, sort it, and be sure the original Seq remains in the right order. In addition, it utilizes Lazy evaluation. http://facebook.github.io/immutable-js/docs/#/Seq
http://facebook.github.io/immutable-js/docs/#/Seq/sortBy

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

[...array].sort(sortFn)