首页 > 代码库 > [Ramda] Sort, SortBy, SortWith in Ramda
[Ramda] Sort, SortBy, SortWith in Ramda
The difference between sort, sortBy, sortWith is that:
1. sort: take function as args.
2. sortBy: take prop as args.
3. sortWith: take array of funcs as args.
const R = require(‘ramda‘);const {sort, sortBy, sortWith, descend, prop, ascend} = R;const sample = [ {name: "Sally", age: 29, height: 65}, {name: "Zac", age: 29, height: 72}, {name: "John", age: 32, height: 61}, {name: "Lisa", age: 28, height: 63}, {name: "Bob", age: 29, height: 66}, {name: "Allen", age: 29, height: 66}];const heightDescending = descend(prop(‘height‘));const ageDescending = descend(prop(‘age‘));const nameAscending = ascend(prop(‘name‘));const sortWithCondition = sortWith([ heightDescending, ageDescending, nameAscending ]);const result = sortWithCondition(sample);/** [ { name: ‘Zac‘, age: 29, height: 72 }, { name: ‘Allen‘, age: 29, height: 66 }, { name: ‘Bob‘, age: 29, height: 66 }, { name: ‘Sally‘, age: 29, height: 65 }, { name: ‘Lisa‘, age: 28, height: 63 }, { name: ‘John‘, age: 32, height: 61 } ]* */console.log(result);/** sort: take function* */const sortByNameDescending = sort(descend(prop(‘name‘)));const result1 = sortByNameDescending(sample);/** [ { name: ‘Zac‘, age: 29, height: 72 }, { name: ‘Sally‘, age: 29, height: 65 }, { name: ‘Lisa‘, age: 28, height: 63 }, { name: ‘John‘, age: 32, height: 61 }, { name: ‘Bob‘, age: 29, height: 66 }, { name: ‘Allen‘, age: 29, height: 66 } ]* */console.log("sortByNameDescending:", result1);/** sortBy: take prop* */const age = prop(‘age‘);const result2 = sortBy(age);/** [ { name: ‘Lisa‘, age: 28, height: 63 }, { name: ‘Sally‘, age: 29, height: 65 }, { name: ‘Zac‘, age: 29, height: 72 }, { name: ‘Bob‘, age: 29, height: 66 }, { name: ‘Allen‘, age: 29, height: 66 }, { name: ‘John‘, age: 32, height: 61 } ]* */console.log(result2(sample));
[Ramda] Sort, SortBy, SortWith in Ramda
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。