Ramda发生意外的"sortBy"行为 [英] Unexpected 'sortBy' Behavior with Ramda

查看:183
本文介绍了Ramda发生意外的"sortBy"行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用'sortBy'会产生意外结果.

Application of 'sortBy' producing unexpected results.

我必须做些愚蠢的事情.这是一个基本的操作.

I've gotta be doing something stoopid. This is such a basic operation.

const input = [4,3,2,1];

const sort = list => R.sortBy(R.ascend(R.identity))(list);

console.log(sort(input)); // [ 4, 3, 2, 1 ]

我希望'console.log'调用的输出为[1,2,3,4],但不是:输出为[4,3,2,1],与输入相同.我究竟做错了什么?

I would expect the output of the 'console.log' invocation to be [ 1, 2, 3, 4 ], but it is not: the output is [ 4, 3, 2, 1 ], same as the input. What am I doing wrong?

推荐答案

正如Aadit M Shah在评论中指出的那样,您没有正确使用sortBy.

As pointed out by Aadit M Shah in the comments you're not using sortBy correctly.

以下是有关如何在Ramda中进行排序的快速概述:

Here's quick overview of how to sort in Ramda:

返回列表的副本,该副本根据比较器功能排序,该副本一次应接受两个值,如果第一个值较小,则返回负数,如果较大则返回正数,如果相等则返回零

Returns a copy of the list, sorted according to the comparator function, which should accept two values at a time and return a negative number if the first value is smaller, a positive number if it's larger, and zero if they are equal.

一种情况下,使用 subtract 进行升序排序:

One case use subtract to sort in ascending order:

sort(subtract, [4, 1, 2, 3]);
//=> [1, 2, 3, 4]

或者以降序排列,只需 flip 即可:

Or to sort in descending, just flip it:

sort(flip(subtract), [4, 1, 2, 3]);
//=> [4, 3, 2, 1]

sort仅仅期望一个函数可以接受两个可以与<>进行比较的参数.

sort simply expects a function that can accept two parameters which can be compared with < or >.

那么您将如何对字符串数组进行排序?可以将字符串与<>进行比较,但使用subtract则没有意义.这是 ascend (或

So how would you sort an array of strings? Strings can be compared with < or > but using subtract wouldn't make sense. This is where ascend (or descend) can be useful:

使返回的值可以与<进行比较的函数中的升序比较器函数.和>.

Makes an ascending comparator function out of a function that returns a value that can be compared with < and >.

sort(ascend(identity), ["b", "a", "B", "A"]);
//=> ["A", "B", "a", "b"]

如果要进行不区分大小写的比较:

And if you want to make a case insensitive comparison:

sort(ascend(toLower), ["b", "a", "B", "A"]);
//=> ["a", "A", "b", "B"]

sortBy

如我们所见,sort希望您为其提供一个函数,该函数接受两个可以使用<>进行比较的参数.可以将数字和字符串与这些运算符进行比较,因此,如果可以直接将它们提供给Ramda,则:

sortBy

As we saw, sort expects that you supply it with a function that accepts two parameters that can be compared together using < or >. Numbers and strings can be compared with these operators so if you can give them to Ramda directly then:

sortBy(identity, [4, 1, 2, 3]);
//=> [1, 2, 3, 4]

与以下相同:

sort(subtract, [4, 1, 2, 3]);
//=> [1, 2, 3, 4]

据我所知,sortBy始终会按升序对事物进行排序.

However as far as I can tell, sortBy will always sort things in ascending order.

当您具有多个排序条件时,请使用sortWith:

You use sortWith when you can have multiple sort criteria:

  • 按年龄升序排列
  • 按名称降序排列
sortWith([ascend(prop('age')), descend(prop('name'))], [
  {age: 40, name: 'John'},
  {age: 40, name: 'Zack'},
  {age: 10, name: 'Liam'},
  {age: 20, name: 'Bill'}
]);
//=> [
//=>   {age: 10, name: "Liam"},
//=>   {age: 20, name: "Bill"},
//=>   {age: 40, name: "Zack"},
//=>   {age: 40, name: "John"}
//=> ]

这篇关于Ramda发生意外的"sortBy"行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆