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));

最新文章

  1. linux系统swappiness参数在内存与交换分区间优化
  2. C++11 auto and decltype
  3. css3实现进度条的模拟
  4. 【软件使用】用IntelliJ IDEA开发Android程序图解
  5. Java web 学习之旅
  6. 基于@AspectJ和schema的aop(三)---切点函数详解
  7. CentOS7 安装Docker报错
  8. MyBatis(3.2.3) - Integration with Spring
  9. [Windows Phone] 在 Windows Phone 8 控制闪光灯
  10. MariaDB数据解压版安装(10.0.16)
  11. Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
  12. 中小研发团队架构实践之分布式协调器ZooKeeper
  13. git 回滚远程服务端master的代码
  14. git与eclipse集成之代码冲突与解决
  15. tidb 架构 ~Tidb学习系列(1)
  16. du 查看文件大小
  17. linux下 gogs的安装和web钩子
  18. Windows MySQL5.7安装和配置
  19. Maven常用命令及Eclipse应用
  20. Java的Spi机制心得

热门文章

  1. setting-在设置中添加新的选项
  2. 动态规划例子:Maximal Square
  3. QQ群功能设计与心理学
  4. 当鼠标聚焦时输入框变色(focus事件实例)
  5. usr/bin/mysqladmin: refresh failed; error: 'Unknown error'
  6. PHP模拟链表操作
  7. Loading half a billion rows into MySQL---转载
  8. ES6的学习之路(基础知识总结)
  9. 快速创建SSH信任实现无密码登录
  10. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)