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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [Ramda] Getter and Setter in Ramda & lens

    Getter on Object: 1. prop: R.prop(}); //=> 100 R.prop('x', {}); //=> undefined 2. props: R.pro ...

  4. [Ramda] Rewrite if..else with Ramda ifElse

    From: const onSeachClick = (searchTerm) => { if(searchTerm !== '') { searchForMovies(searchTerm) ...

  5. scala sortBy and sortWith

    sortBy: sortBy[B](f: (A) ⇒ B)(implicit ord: math.Ordering[B]): List[A] 按照应用函数f之后产生的元素进行排序 sorted: so ...

  6. 前端笔记之React(六)ES6的Set和Map&immutable和Ramda和lodash&redux-thunk

    一.ES6的Set.Map数据结构 Map.Set都是ES6新的数据结构,都是新的内置构造函数,也就是说typeof的结果,多了两个: Set 是不能重复的数组 Map 是可以任何东西当做键的对象 E ...

  7. javascript的sort()方法

    定义和用法: sort() 方法用于对数组的元素进行排序. 语法: 1 arrayObject.sort(sortby) 描述: sortby    可选.必须是函数.规定排序顺序  . 返回值: 对 ...

  8. Js数组排序函数sort()

    JS实现多维数组和对象数组排序,用的其实就是原生sort()函数,语法为:arrayObject.sort(sortby)(sortby 可选.规定排序顺序.必须是函数.) 返回值为对数组的引用:请注 ...

  9. 神奇的sort()函数

    今天来谈一谈sort()函数,sort() 方法用于对数组的元素进行排序,用法为arrayObject.sort(sortby):括号中的为可选参数,准确来说应该是一个函数,这个函数用来规定排序方法, ...

随机推荐

  1. libcurl 上传文件至 web服务器

    测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) libcurl vc6 工程代码  下载地址:  http://download.csdn.ne ...

  2. js进阶 13 jquery动画函数有哪些

    js进阶 13 jquery动画函数有哪些 一.总结 一句话总结: 二.jquery动画函数有哪些 原生JavaScript编写动画效果代码比较复杂,而且还需要考虑兼容性.通过jQuery,我们使用简 ...

  3. Project Euler 613 Pythagorean Ant(概率+积分)

    题目链接:点击我打开题目链接 题目大意: 给你一只蚂蚁,它在一个 边长为 \(30-40-50\) 的直角三角形\((x,y)\)上,并且它在直角三角形中选择的位置和移动方向的概率都是相等的.问你这只 ...

  4. js循环函数中的匿名函数和闭包问题(匿名函数要用循环中变量的问题)

    js循环函数中的匿名函数和闭包问题(匿名函数要用循环中变量的问题) 一.总结 需要好好看下面代码 本质是因为匿名函数用到了循环中的变量,而普通方式访问的话,匿名函数的访问在循环之后,所以得到的i是循环 ...

  5. memcached缓存分布式部署方案

    一.分布式方案介绍 比较流行的两种方案: 1.取余分布: 计算key的哈希值,与服务器数量取余,得到目标服务器.优点:实现简单,当某台服务器不可用时,故障转移方便:缺点:当增减服务器时, Key与服务 ...

  6. 表单提交数据格式form data

    前言: 最近遇到的最多的问题就是表单提交数据格式问题了. 常见的三种表单提交数据格式,分别举例说明:(项目是vue的框架) 1.application/x-www-form-urlencoded 提交 ...

  7. 清除浮动.md

    清除浮动的三种方法 1 加空div层(.clear) 2 overflow属性设置(.clearo) 3 :after伪元素(.clearfix) <!DOCTYPE html> < ...

  8. 被误解的MVC和被神化的MVVM

    MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架 ...

  9. addSubview 与 removeFromSuperview

    //当前视图的父视图添加和本视图同级的视图 [self.view.superview addSubview:showview.view]; //从父视图移除当前视图 [self.view remove ...

  10. lv resize

    # lvreduce -L -400G /dev/vg_atalinux001/lv_home # resize2fs   /dev/vg_atalinux001/lv_home resize2fs ...