mori库的sortBy稳定吗?

Is sortBy of the mori library stable?

是mori库的sortby

http://swannodette.github.io/mori/#sortBy

稳定吗?

(在这个意义上稳定:https://softwareengineering.stackexchange.com/questions/247440/what-does-it-mean-for-a-sorting-algorithm-to-be-stable

简而言之 - 是的

文档中没有描述,但是... Mori 使用 Clojure Script 中的排序依据(mori 存储库中没有其他 sortBy 实现) https://github.com/swannodette/mori/blob/master/src/mori.cljs#L30

(mori-export sortBy cljs.core/sort-by)

cljs实现sort-by https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L2328

它使用了Google声称稳定的闭包库 https://github.com/google/closure-library/blob/master/closure/goog/array/array.js#L1144

这里有一个测试脚本来检查稳定性:

// Perform tests (increase number for thorough test):
for (var i = 0; i < 4; i++) {
    // Create random array of zeroes and ones, and pair them with a sequence number:
    const arr = Array.from(Array(10), (_, i) => [+(Math.random()>=0.5), i]);
    console.log('in: ', JSON.stringify(arr));
    // Sort by the 0 and 1 values:
    const result = mori.toJs(mori.sortBy(v => v[0], arr));
    console.log('out:', JSON.stringify(result));
    // Throw an error if the sequence number of equal values is not increasing
    result.reduce( (a, b) => {
        if (a[0] === b[0] && a[1] > b[1]) throw "Not stable!";
        return b;
    } );
}
console.log('is stable');
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mori/0.3.2/mori.min.js"></script>

当对大型数组进行多次尝试时,事实证明排序始终保持具有相同排序顺序的值的原始顺序。所以稳定。