zoukankan      html  css  js  c++  java
  • [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));
  • 相关阅读:
    repo sync中遇到:contains uncommitted changes
    <kernel>/scripts/checkpatch.pl脚本可用来检查代码书写不规范和作一些简单的代码静态检查
    各国股市开盘与收盘时间
    分页数据绑定例子模板
    提升网络销售转化率的10种方法
    网络业务员
    股票入门:如何看盘
    带样式的页码代码
    看着一年一度的高考,虽然高考已经离我远去
    ajax处理函数模板代码
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6510712.html
Copyright © 2011-2022 走看看