const makeSorter = (sortConfig) =>R.sort ((sortConfig.direction === 'descending' ? R.descend : R.ascend) ( R.compose (sortConfig.isNumber ?Number.parseFloat : R.toLower,R.prop (sortConfig.column))))常量人 = [{名称:'弗雷德',年龄:25},{名称:'巴尼',年龄:28},{名称:'威尔玛',年龄:29},{名称:'贝蒂',年龄:22}]const byName = {direction: 'descending', column: 'name', isNumber: false}const getSortedData = makeSorter(byName)控制台 .log (getSortedData(人))const byAge = {direction: 'ascending', column: 'age', isNumber: true}控制台 .log (makeSorter(按年龄)(人))<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>我个人更喜欢用解构来编写它,例如:const makeSorter = ({direction, isNumber, column}) =>R.sort ((direction === 'descending' ? R.descend : R.ascend) ( R.compose (是号码?Number.parseFloat : R.toLower,R.prop(列))))但这并没有改变任何基本的东西.I have a React component with a sorting headers table. This code works as planned: //definitions and imports... const [sort, setSort] = useState({column: 'rank', direction: 'ascending', isNumber: true}); const handleSort = (column, isNumber = false) => () => { let direction = (sort.column !== column) ? 'ascending' : (sort.direction === 'ascending' ? 'descending' : 'ascending'); setSort({column, direction, isNumber}); }; const getSortedData = (data) => { let sorted = R.sortBy( R.compose( sort.isNumber ? Number.parseFloat : R.toLower, R.prop(sort.column) ), data ); return sort.direction === 'ascending' ? sorted : sorted.reverse(); } //rest of the component...However, I'm sure there's a way to use R.compose to conditionally ascend or descend the sort (based on the sort.direction property), and thus allow me to remove the last line of getSortedData and the reverse array op.I tried several things (like composing ascend with prop, using the conditional in the compose etc.), but it all breaks down the code.While it works well now, can anyone help me make it more Ramdaish?Update - adding the solution here for posterity's sake:const getSortedData = (data) => { return R.sort( (sort.direction === 'ascending' ? R.ascend : R.descend)( R.compose( sort.isNumber ? Number.parseFloat : R.toLower, R.prop(sort.column) ) ) )(data);}Things I've learned:R.sort returns a function, and not an array, so you need to send data in as a parameter to the whole function.R.ascend/R.descend also expect the function as a paremeter, and therefore should not be a parameter of R.compose on their own. 解决方案 I think I would do something like this, wrapping ascend or descend around the composition:const makeSorter = (sortConfig) => R.sort ( (sortConfig.direction === 'descending' ? R.descend : R.ascend) ( R.compose ( sortConfig.isNumber ? Number.parseFloat : R.toLower, R.prop (sortConfig.column) )))const people = [ {name: 'fred', age: 25}, {name: 'barney', age: 28}, {name: 'wilma', age: 29}, {name: 'betty', age: 22}]const byName = {direction: 'descending', column: 'name', isNumber: false}const getSortedData = makeSorter(byName)console .log ( getSortedData (people))const byAge = {direction: 'ascending', column: 'age', isNumber: true}console .log ( makeSorter (byAge) (people))<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>I would personally prefer to write it with destructuring, as such:const makeSorter = ({direction, isNumber, column}) => R.sort ( (direction === 'descending' ? R.descend : R.ascend) ( R.compose ( isNumber ? Number.parseFloat : R.toLower, R.prop (column) )))but that doesn't change anything fundamental. 这篇关于如何在 Ramda 中组合升序/降序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-16 14:01
查看更多