[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的更多相关文章
- [Ramda] Handle Branching Logic with Ramda's Conditional Functions
When you want to build your logic with small, composable functions you need a functional way to hand ...
- [Ramda] Change Object Properties with Ramda Lenses
In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus ...
- [Ramda] Getter and Setter in Ramda & lens
Getter on Object: 1. prop: R.prop(}); //=> 100 R.prop('x', {}); //=> undefined 2. props: R.pro ...
- [Ramda] Rewrite if..else with Ramda ifElse
From: const onSeachClick = (searchTerm) => { if(searchTerm !== '') { searchForMovies(searchTerm) ...
- scala sortBy and sortWith
sortBy: sortBy[B](f: (A) ⇒ B)(implicit ord: math.Ordering[B]): List[A] 按照应用函数f之后产生的元素进行排序 sorted: so ...
- 前端笔记之React(六)ES6的Set和Map&immutable和Ramda和lodash&redux-thunk
一.ES6的Set.Map数据结构 Map.Set都是ES6新的数据结构,都是新的内置构造函数,也就是说typeof的结果,多了两个: Set 是不能重复的数组 Map 是可以任何东西当做键的对象 E ...
- javascript的sort()方法
定义和用法: sort() 方法用于对数组的元素进行排序. 语法: 1 arrayObject.sort(sortby) 描述: sortby 可选.必须是函数.规定排序顺序 . 返回值: 对 ...
- Js数组排序函数sort()
JS实现多维数组和对象数组排序,用的其实就是原生sort()函数,语法为:arrayObject.sort(sortby)(sortby 可选.规定排序顺序.必须是函数.) 返回值为对数组的引用:请注 ...
- 神奇的sort()函数
今天来谈一谈sort()函数,sort() 方法用于对数组的元素进行排序,用法为arrayObject.sort(sortby):括号中的为可选参数,准确来说应该是一个函数,这个函数用来规定排序方法, ...
随机推荐
- android-5.1编译配置(van)
必备文件: archives1211.tgz ubuntu_install_1204.tgz 安装指引: ubuntu_install_1204/readme.txt 工作目录结构: git ├── ...
- Java 函数的参数说
java函数参数传递的到底是值还是引用对确实容易让人迷糊.而很多时候因为对这个问题的模糊甚至造成一些错误.最常见的说法是基本类型传的是值,对象传的引用.对于基本类型,大家都达成共识,没有什么可以争论的 ...
- Java基础学习总结(52)——Liunx系统Centos上搭建Java开发环境
一.安装jdk 1.查看Linux自带的JDK是否已安装 [plain] view plain copy print? java –version 如果出现openjdk,最好还是先卸载掉openjd ...
- Java Web学习总结(15)——JSP指令
一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...
- 主定理(Master Theorem)与时间复杂度
1. 问题 Karatsuba 大整数的快速乘积算法的运行时间(时间复杂度的递推关系式)为 T(n)=O(n)+4⋅T(n/2),求其最终的时间复杂度. 2. 主定理的内容 3. 分析 所以根据主定理 ...
- 关于DOM的有关总结
1.获取DOM元素 document.getElementById() 通过id获取DOM元素 document.getElementsByClassName() 通过类名获取DOM元素 docum ...
- RMAN冷备份、一致性备份脚本
RMAN冷备份.一致性备份脚本 run{ shutdown immediate; startup mount; allocate channel c1 type disk; allocate chan ...
- Ab工具基本使用
Ab简介 ab是apache自带的压力测试工具,ab是apachebench命令的缩写. ab不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试. ab是一个ht ...
- Mosquito的优化——订阅树优化(八)
本文由逍遥子撰写.转发请标注原址: http://blog.csdn.net/houjixin/article/details/46413783 或 http://houjixin.blog.163. ...
- 想在子线程里面触发的信号的槽函数在子线程执行,信号槽连接必须使用DirectConnection 方式(即使跨线程,也可以强迫DirectConnection,而不能是AutoConnection)
Qt多线程的实现 1.继承QThread,重新run 2.继承Object,调用moveToThread方法 两种方法各有利弊:主要参考:http://blog.51cto.com/9291927/1 ...