mirror of
https://github.com/swc-project/swc.git
synced 2024-12-26 23:27:56 +03:00
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/sortBy.js
|
||
|
|
||
|
|
||
|
import _curry2 from './internal/_curry2.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Sorts the list according to the supplied function.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.1.0
|
||
|
* @category Relation
|
||
|
* @sig Ord b => (a -> b) -> [a] -> [a]
|
||
|
* @param {Function} fn
|
||
|
* @param {Array} list The list to sort.
|
||
|
* @return {Array} A new list sorted by the keys generated by `fn`.
|
||
|
* @example
|
||
|
*
|
||
|
* const sortByFirstItem = R.sortBy(R.prop(0));
|
||
|
* const pairs = [[-1, 1], [-2, 2], [-3, 3]];
|
||
|
* sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
|
||
|
*
|
||
|
* const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
|
||
|
* const alice = {
|
||
|
* name: 'ALICE',
|
||
|
* age: 101
|
||
|
* };
|
||
|
* const bob = {
|
||
|
* name: 'Bob',
|
||
|
* age: -10
|
||
|
* };
|
||
|
* const clara = {
|
||
|
* name: 'clara',
|
||
|
* age: 314.159
|
||
|
* };
|
||
|
* const people = [clara, bob, alice];
|
||
|
* sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
|
||
|
*/
|
||
|
var sortBy = _curry2(function sortBy(fn, list) {
|
||
|
return Array.prototype.slice.call(list, 0).sort(function(a, b) {
|
||
|
var aa = fn(a);
|
||
|
var bb = fn(b);
|
||
|
return aa < bb ? -1 : aa > bb ? 1 : 0;
|
||
|
});
|
||
|
});
|
||
|
export default sortBy;
|