- https://ramdajs.com/docs/#sort">https://ramdajs.com/docs/#sort
- https://ramdajs.com/docs/#sortBy 单条件">https://ramdajs.com/docs/#sortBy 单条件
- https://ramdajs.com/docs/#sortWith 多条件">https://ramdajs.com/docs/#sortWith 多条件
- https://ramdajs.com/docs/#ascend 升序">https://ramdajs.com/docs/#ascend 升序
- https://ramdajs.com/docs/#descend 降序">https://ramdajs.com/docs/#descend 降序
- JS默认的排序是具有破坏性的
- Ramda提供一个没有破坏性的排序函数
https://ramdajs.com/docs/#sort
sort 接受一个List
((a, a) → Number) → [a] → [a]
Parameters
Added in v0.1.0
Returns a copy of the list, sorted according to the comparator function, which should accept two values at a time and return a negative number if the first value is smaller, a positive number if it’s larger, and zero if they are equal. Please note that this is a copy of the list. It does not modify the original.
const diff = function(a, b) { return a - b; };
R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
https://ramdajs.com/docs/#sortBy 单条件
sortBy 按照给定逻辑去排序Relation
Ord b => (a → b) → [a] → [a]
Parameters
Added in v0.1.0
Sorts the list according to the supplied function.
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]
https://ramdajs.com/docs/#sortWith 多条件
sortWith 按照一个数组的逻辑Relation去排序
[(a, a) → Number] → [a] → [a]
Parameters
Added in v0.23.0
Sorts a list according to a list of comparators.
const alice = {
name: 'alice',
age: 40
};
const bob = {
name: 'bob',
age: 30
};
const clara = {
name: 'clara',
age: 40
};
const people = [clara, bob, alice];
const ageNameSort = R.sortWith([
R.descend(R.prop('age')),
R.ascend(R.prop('name'))
]);
ageNameSort(people); //=> [alice, clara, bob]
https://ramdajs.com/docs/#ascend 升序
ascend Function
Ord b => (a → b) → a → a → Number
Parameters
Added in v0.23.0
Makes an ascending comparator function out of a function that returns a value that can be compared with < and >.
See also descend.
上升函数 作战需求文件b=>(a→ (b)→ A.→ A.→ 数 参数 添加到v0.23.0中
从返回可与<和>进行比较的值的函数中生成升序比较器函数。 见下。
https://ramdajs.com/docs/#descend 降序
Ord b => (a → b) → a → a → Number
Parameters
Added in v0.23.0
Makes a descending comparator function out of a function that returns a value that can be compared with < and >.
See also ascend.
Ord b => (a → b) → a → a → Number
Parameters
Added in v0.23.0
Makes a descending comparator function out of a function that returns a value that can be compared with < and >.
See also ascend.
const byAge = R.descend(R.prop('age'));
const people = [
{ name: 'Emma', age: 70 },
{ name: 'Peter', age: 78 },
{ name: 'Mikhail', age: 62 },
];
const peopleByOldestFirst = R.sort(byAge, people);
//=> [{ name: 'Peter', age: 78 }, { name: 'Emma', age: 70 }, { name: 'Mikhail', age: 62 }]
JS默认的排序是具有破坏性的
本机排序是破坏性的,这意味着它会变异数组。
The native sort is destructive, meaning it mutates the array.
const nums = [3, 2, 4, 1];
const result = nums.sort((a, b) => a - b)
console.log({ nums, result });
Ramda提供一个没有破坏性的排序函数
Ramda provides a non-destructive sort function.
import { sort } from 'ramda';
const nums = [3, 2, 4, 1];
const result = sort((a, b) => a - b, nums);
console.log({ nums, result });
{ nums: [ 3, 2, 4, 1 ], result: [ 1, 2, 3, 4 ] }
sortBy applies a function to your values before comparing them. Sorting by absolute value, for example, is trivial now.
sortBy在比较值之前将函数应用于值。
例如,按绝对值排序现在就很简单了。
import { sortBy } from 'ramda';
const result = sortBy(Math.abs, [-100, 1, -50, 0]);
console.log({ result });
{ result: [ 0, 1, -50, -100 ] }
If you’d like to sort by multiple criteria, sortWith is your friend. This example sorts people by their age, then name.
如果您想按多个条件排序,sortWith是您的朋友。此示例按年龄对人进行排序,然后按姓名进行排序。
import { ascend, prop, sortWith } from 'ramda';
const people = [{
name: 'Bobo',
age: 25
}, {
name: 'Cam',
age: 25
}, {
name: 'Al',
age: 29
}];
const result = sortWith([
ascend(prop('age')),
ascend(prop('name'))
], people);
console.log({ result });
{ result:
[ { name: 'Bobo', age: 25 },
{ name: 'Cam', age: 25 },
{ name: 'Al', age: 29 } ] }
Ramda’s ascend functions wraps around prop(‘age’), telling it “Sort by age, ascending (smallest first)”.
Ramda的ascend函数围绕道具(“年龄”),告诉它“按年龄排序,升序(最小的优先)”。
Same with prop(‘name’): “Sort by name, ascending (A, B, C, D…).”
ascend(prop('age'))
ascend(prop('name'))
So if they’re the same age, sortWith will then sort by name.
For the opposite effect, use descend.
import { descend, prop, sortWith } from 'ramda';
const people = [{
name: 'Bobo',
age: 25
}, {
name: 'Cam',
age: 25
}, {
name: 'Al',
age: 29
}];
const result = sortWith([
descend(prop('age')),
descend(prop('name'))
], people);
console.log({ result });
{ result:
[ { name: 'Al', age: 29 },
{ name: 'Cam', age: 25 },
{ name: 'Bobo', age: 25 } ] }