[Ramda] Sort, SortBy, SortWith in Ramda
2024-09-08 10:34:18
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));
最新文章
- linux系统swappiness参数在内存与交换分区间优化
- C++11 auto and decltype
- css3实现进度条的模拟
- 【软件使用】用IntelliJ IDEA开发Android程序图解
- Java web 学习之旅
- 基于@AspectJ和schema的aop(三)---切点函数详解
- CentOS7 安装Docker报错
- MyBatis(3.2.3) - Integration with Spring
- [Windows Phone] 在 Windows Phone 8 控制闪光灯
- MariaDB数据解压版安装(10.0.16)
- Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
- 中小研发团队架构实践之分布式协调器ZooKeeper
- git 回滚远程服务端master的代码
- git与eclipse集成之代码冲突与解决
- tidb 架构 ~Tidb学习系列(1)
- du 查看文件大小
- linux下 gogs的安装和web钩子
- Windows MySQL5.7安装和配置
- Maven常用命令及Eclipse应用
- Java的Spi机制心得
热门文章
- setting-在设置中添加新的选项
- 动态规划例子:Maximal Square
- QQ群功能设计与心理学
- 当鼠标聚焦时输入框变色(focus事件实例)
- usr/bin/mysqladmin: refresh failed; error: &;#39;Unknown error&;#39;
- PHP模拟链表操作
- Loading half a billion rows into MySQL---转载
- ES6的学习之路(基础知识总结)
- 快速创建SSH信任实现无密码登录
- 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)