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. 【MySQL集群】——Java程序连接MySQL集群

    上篇简介了怎样在Windows环境下建立配置MySQL集群,这里用一个实现注冊功能的小Demo通过jdbc的方式连接到MySQL集群中. 外部程序想要远程连接到mysql集群,还须要做的一个操作就是设 ...

  2. ActivityChooserView-如何隐藏选择的应用图标

    今天在修改一个问题的时候,用到了ActivityChooserView类,但是,这个类会自动显示两个按钮,一个是点击有下拉框的,一个是选择应用以后,显示应用图标的.因为应用图标跟当时的环境非常的不搭, ...

  3. Linux体系结构

    linux内核结构: system call interface (SCI层) 为用户空间提供了一套标准的系统调用函数来访问linux内核. process management (PM层) 进程管理 ...

  4. session 、cookie、token的区别及联系

    本文转自:https://blog.csdn.net/jikeehuang/article/details/51488020:https://blog.csdn.net/weixin_37196194 ...

  5. Makefile的问题

    注意包含路径的变量,可能会带有空格而使得运行失败 直接定义:DEBUG  = false后面引用需要使用 $(DEBUG) 或者set  DEBUG  $(DEBUG)之后就可以直接引用了

  6. MyBatis学习总结(15)——定制Mybatis自动代码生成的maven插件

    ==================================================================================================== ...

  7. 关于js盒子模型的知识梳理

    盒子模型 JS盒子模型中的13个常用属性: clientWidth/clientHeight:可视区域的宽高,宽高+PADDING组成 clientTop/clientLeft:上边框和左边框的宽度 ...

  8. SpringMVC响应Ajax请求(@Responsebody注解返回页面)

    项目需求描述:page1中的ajax请求Controller,Controller负责将service返回的数据填充到page2中,并将page2整个页面返回到page1中ajax的回调函数. 一句话 ...

  9. 【习题5-3 UVA-10935】Throwing cards away I

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用STL的queue写 [代码] #include <bits/stdc++.h> using namespace st ...

  10. 在 AppDelegate 设置屏幕切换

    //禁止横屏显示 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWin ...