最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you sort an array of words with Ramda? - Stack Overflow

programmeradmin1浏览0评论

Sorting numbers is easy with Ramda.

const sizes = ["18", "20", "16", "14"]
console.log("Sorted sizes", R.sort((a, b) => a - b, sizes))
//=> [ '14', '16', '18', '20' ]

Sorting an array of words with vanilla javascript is too.

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", trees.sort())

How would you sort an array of words with Ramda.
If you had to.

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", R.sort((a, b) => a - b, trees))
//=> ["cedar", "elm", "willow", "beech"]

Sorting numbers is easy with Ramda.

const sizes = ["18", "20", "16", "14"]
console.log("Sorted sizes", R.sort((a, b) => a - b, sizes))
//=> [ '14', '16', '18', '20' ]

Sorting an array of words with vanilla javascript is too.

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", trees.sort())

How would you sort an array of words with Ramda.
If you had to.

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", R.sort((a, b) => a - b, trees))
//=> ["cedar", "elm", "willow", "beech"]
Share Improve this question asked Sep 30, 2018 at 19:24 SifnosSifnos 1,1712 gold badges13 silver badges22 bronze badges 3
  • 1 Why wouldn't you just use native method? – charlietfl Commented Sep 30, 2018 at 19:28
  • 2 @charlietfl Might be because the native method requires mutation of an existing object, unlike R.sort – CertainPerformance Commented Sep 30, 2018 at 19:29
  • I guess the native sort mutates the current list whereas ramda sort returns a copy. – Anirudh Mangalvedhekar Commented Sep 30, 2018 at 19:29
Add a comment  | 

4 Answers 4

Reset to default 15

Don't try to subtract strings - instead, use localeCompare to check whether one string comes before another alphabetically:

const trees = ["cedar", "elm", "willow", "beech"]
console.log("Sorted trees", R.sort((a, b) => a.localeCompare(b), trees))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

You can create a comparator with R.comparator and R.lt:

const trees = ["cedar", "elm", "willow", "beech"]
const result = R.sort(R.comparator(R.lt), trees)
console.log("Sorted trees", result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

import R from 'ramda'

const names = ['Khan', 'Thanos', 'Hulk']

const sortNamesAsc = R.sortBy(R.identity) // alphabetically
const sortNamesDesc = R.pipe(sortNamesAsc, R.reverse)

sortNamesAsc(names) // ['Hulk', 'Khan', 'Thanos']
sortNamesDesc(names) // ['Thanos', 'Khan', 'Hulk']

Ramda Repl example

Is this what you mean by sort an array of words with Ramda?

import R from 'ramda'
var objs = [ 
    { first_name: 'x', last_name: 'a'     },
    { first_name: 'y',    last_name: 'b'   },
    { first_name: 'z', last_name: 'c' }
];
var ascendingSortedObjs = R.sortBy(R.prop('last_nom'), objs)
var descendingSortedObjs = R.reverse(ascendingSortedObjs)
发布评论

评论列表(0)

  1. 暂无评论