[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));
时间: 2024-08-24 07:52:33

[Ramda] Sort, SortBy, SortWith in Ramda的相关文章

[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 changes on specific properties of an object while keeping your data immutable. what 'R.lens' do is able to get or set prop value but keep the object i

[Ramda] Rewrite if..else with Ramda ifElse

From: const onSeachClick = (searchTerm) => { if(searchTerm !== '') { searchForMovies(searchTerm) } else { console.log('a search term should be provided') } } To: // Utils const inNotEmpty = R.compose( R.not, R.isEmpty ); const onSearchClick = () =>

Scala--数组类型

1.定长数组Array scala> val a = new Array[Int](10)a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) Int类型的数组的默认值是0 --------------------------------------------------------------- scala> val a = new Array[String](10)a: Array[String] = Array(null, null,

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js libs

Tansducer-js lib A high performance Transducers implementation for JavaScript. Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transfo

神奇的sort()函数

今天来谈一谈sort()函数,sort() 方法用于对数组的元素进行排序,用法为arrayObject.sort(sortby):括号中的为可选参数,准确来说应该是一个函数,这个函数用来规定排序方法,不然sort怎么知道你想怎么排,从大到小还是从小到大,你不跟它说它只能按它自己的方法排,如果你对它不熟悉的话,排出来的结果分分钟让你懵逼,需要说明的是,它是在原数组上排序的,不生成副本. 排序方法:如果你不给它指定方法的话,它会按照字符编码的顺序进行排序,对数字的话排出来基本没什么卵用,所以你要提供

JavaScript数组sort()方法小结

sort语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 由于sort方法是先将数组元素转换为字符串进行比较,根据字符串首字符的ASCII码排序进行比较,所以有时候不能满足我们对数组数字集合的排序要求,但是sort()方法可以采用函数,利用冒泡法对数组进行排序,我个人的理解是如果要对其进行排序给定函数有以下两种格式: Array.sort(function(a,b){ a-b; }) 此种方法实现由小到大排序. Array.sort(fun

javascript函数sort

sort函数用于对数组的元素进行排序 语法:arrayObject.sort(sortby); 参数sortby可选,规定排序顺序,必须是函数. 如果调用该方法时没有使用参数,将按照字幕顺序进行排序,就是按照字母的编码顺序进行排序.要实现这一点,就要将数组的元素转换成字符串,以便进行比较. 如果要按照其他标准进行排序,就需要提供一个比较函数,该函数要比较两个值,然后返回一个用于说明这两个数相对位置的数字: 比较函数应该具有两个参数a和b,其返回值:若a小于b,在排序后的数字中a应该出现在b之前,

Javascript:sort()方法快速实现对数组排序

定义和用法: sort() 方法用于对数组的元素进行排序. 语法: arrayObject.sort(sortby) 注释:sortby,可选,规定排序顺序,必须是函数. 说明: 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序. 要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以便进行比较. 栗子: 应用在如上所示的单个数字或者字母的场景时,sort() 方法圆满的完成了任务: 但,在处理下面栗子中所展示的数组时却出现了异

js中sort()方法的用法,参数以及排序原理

sort() 方法用于对数组的元素进行排序. 语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 注:如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以便进行比较. 如果想按照其他标准进行排序,就需要提供比较函数,该函数要比较两个值,然后返回一个用于说明这两个值的相对顺序的数字.比较函数应该具有两个参数 a 和 b,其返回