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.

  1. const diff = function(a, b) { return a - b; };
  2. 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.

  1. const sortByFirstItem = R.sortBy(R.prop(0));
  2. const pairs = [[-1, 1], [-2, 2], [-3, 3]];
  3. sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
  4. const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
  5. const alice = {
  6. name: 'ALICE',
  7. age: 101
  8. };
  9. const bob = {
  10. name: 'Bob',
  11. age: -10
  12. };
  13. const clara = {
  14. name: 'clara',
  15. age: 314.159
  16. };
  17. const people = [clara, bob, alice];
  18. 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.

  1. const alice = {
  2. name: 'alice',
  3. age: 40
  4. };
  5. const bob = {
  6. name: 'bob',
  7. age: 30
  8. };
  9. const clara = {
  10. name: 'clara',
  11. age: 40
  12. };
  13. const people = [clara, bob, alice];
  14. const ageNameSort = R.sortWith([
  15. R.descend(R.prop('age')),
  16. R.ascend(R.prop('name'))
  17. ]);
  18. 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.

  1. const byAge = R.descend(R.prop('age'));
  2. const people = [
  3. { name: 'Emma', age: 70 },
  4. { name: 'Peter', age: 78 },
  5. { name: 'Mikhail', age: 62 },
  6. ];
  7. const peopleByOldestFirst = R.sort(byAge, people);
  8. //=> [{ name: 'Peter', age: 78 }, { name: 'Emma', age: 70 }, { name: 'Mikhail', age: 62 }]

JS默认的排序是具有破坏性的

本机排序是破坏性的,这意味着它会变异数组。
The native sort is destructive, meaning it mutates the array.

  1. const nums = [3, 2, 4, 1];
  2. const result = nums.sort((a, b) => a - b)
  3. console.log({ nums, result });

Ramda提供一个没有破坏性的排序函数

Ramda provides a non-destructive sort function.

  1. import { sort } from 'ramda';
  2. const nums = [3, 2, 4, 1];
  3. const result = sort((a, b) => a - b, nums);
  4. console.log({ nums, result });
  5. { 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在比较值之前将函数应用于值。
例如,按绝对值排序现在就很简单了。

  1. import { sortBy } from 'ramda';
  2. const result = sortBy(Math.abs, [-100, 1, -50, 0]);
  3. console.log({ result });
  4. { 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是您的朋友。此示例按年龄对人进行排序,然后按姓名进行排序。

  1. import { ascend, prop, sortWith } from 'ramda';
  2. const people = [{
  3. name: 'Bobo',
  4. age: 25
  5. }, {
  6. name: 'Cam',
  7. age: 25
  8. }, {
  9. name: 'Al',
  10. age: 29
  11. }];
  12. const result = sortWith([
  13. ascend(prop('age')),
  14. ascend(prop('name'))
  15. ], people);
  16. console.log({ result });
  1. { result:
  2. [ { name: 'Bobo', age: 25 },
  3. { name: 'Cam', age: 25 },
  4. { 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…).”

  1. ascend(prop('age'))
  2. ascend(prop('name'))

So if they’re the same age, sortWith will then sort by name.
For the opposite effect, use descend.

  1. import { descend, prop, sortWith } from 'ramda';
  2. const people = [{
  3. name: 'Bobo',
  4. age: 25
  5. }, {
  6. name: 'Cam',
  7. age: 25
  8. }, {
  9. name: 'Al',
  10. age: 29
  11. }];
  12. const result = sortWith([
  13. descend(prop('age')),
  14. descend(prop('name'))
  15. ], people);
  16. console.log({ result });
  1. { result:
  2. [ { name: 'Al', age: 29 },
  3. { name: 'Cam', age: 25 },
  4. { name: 'Bobo', age: 25 } ] }