Unexpected 'sortBy' Behavior with Ramda

倾然丶 夕夏残阳落幕 提交于 2020-01-03 05:13:09

问题


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 ]

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?


回答1:


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

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

sort

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.

One case use subtract to sort in ascending order:

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

Or to sort in descending, just flip it:

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

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

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

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]

is the same as:

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

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:

  • Sort by age in ascending order
  • Sort by name in descending order
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"}
//=> ]


来源:https://stackoverflow.com/questions/56105016/unexpected-sortby-behavior-with-ramda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!