[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):括号中的为可选参数,准确来说应该是一个函数,这个函数用来规定排序方法, ...
随机推荐
- 【CS Round #43 A】Expected Dice
[链接]https://csacademy.com/contest/round-43/task/expected-dice/ [题意] 大水题 [题解] 把36种可能的结果都存下来. 然后把重复出现的 ...
- 【CS Round #46 (Div. 1.5) E】Ultimate Orbs
[链接]链接 [题意] n个人从左到右站在一条直线上.每个人都有一个能力值g[i],然后每个人可以将相邻的一个人打败. 然后它的能力值能够增加相应的能力值(就是打败了的那个人的能力值). A能够打败B ...
- Java Web学习总结(5)——HttpServletResponse对象详解
一.HttpServletResponse常见应用--生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: package gacl.r ...
- Android SimpleAdapter
1.MainActivity.java public class MainActivity extends Activity { private ListView listView; private ...
- JS学习笔记 - 点击、回车、ctrl+回车提交留言
疑点: oTxt1.onkeydown = function (ev) 为什么这里的onkeydown = function有变量 (ev),前面onclick函数没有? window.onload ...
- Kinect开发笔记之三Kinect开发环境配置具体解释
0.前言: 首先说一下我的开发环境,Visual Studio是2013的,系统是win8的64位版本号,SDK是Kinect for windows SDK 1.8版本 ...
- linux安装anaconda
打开网址:https://repo.continuum.io/archive/ 下载对应版本: 然后把下载的文件放到linux系统上 在终端执行: bash Anaconda3-5.1.0-Linux ...
- Mac 环境下svn服务器的配置
Mac 环境下svn服务器的配置 本文目录 • 一.创建代码仓库,用来存储客户端所上传的代码 • 二.配置svn的用户权限 • 三.使用svn客户端功能 在Windows环境中,我们一般使用Torto ...
- 【习题 3-6 UVA - 232】Crossword Answers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.注意场宽为3 [代码] #include <bits/stdc++.h> using namespace std ...
- windows+python3+opencv3.4安装
1.安装anaconda. 2.pip install opencv-python 网上很多关于python opencv安装说明,步骤极其繁琐,其实按照本说明只需两步就可安装完成.