使用 ramda sortWith 进行不区分大小写的排序

Case insensitive sorting with ramda sortWith

我在使用 Ramda sortWith 按多列(升序或降序)对深度嵌套的对象数组进行排序时遇到问题。显然,排序区分大小写,导致以小写字母开头的值被放在排序数组的最后。

首先我导入必要的 ramda 函数:

import { sortWith, path, useWith, split, ascend, descend } from 'ramda'

然后我有这个数组:

const theList = [
  {
    name: 'A',
    purchase: {
      period: {
        start: '2020-01-08T21:00:00Z',
        end: '3070-10-27T21:00:00Z',
      },
    },
  },
  {
    name: 'b',
    purchase: {
      period: {
        start: '2019-09-30T19:00:00Z',
        end: '3070-10-27T21:00:00Z',
      },
    },
  },
  {
    name: 'C',
    purchase: {
      period: {
        start: '2020-01-26T21:00:00Z',
        end: '3070-10-27T21:00:00Z',
      },
    },
  },
]

为了排序,我有这个带有规则的数组:

const sort = [
  { oder: 'asc', name: 'name' },
  { oder: 'desc', name: 'purchase.period.start' },
]

最后,我尝试使用给定的规则集对这个数组进行排序:

const sortFunctions = sort.map(({ order, name }) => (
  (order === 'asc')
    ? ascend(useWith(path, [split('.')])(name))
    : descend(useWith(path, [split('.')])(name))
))

return sortWith(sortFunctions)(theList)

然而结果如下:

[
  {
    "name": "b",
    "purchase": {
      "period": {
        "start": "2019-09-30T19:00:00Z",
        "end": "3070-10-27T21:00:00Z"
      }
    }
  },
  {
    "name": "C",
    "purchase": {
      "period": {
        "start": "2020-01-26T21:00:00Z",
        "end": "3070-10-27T21:00:00Z"
      }
    }
  },
  {
    "name": "A",
    "purchase": {
      "period": {
        "start": "2020-01-08T21:00:00Z",
        "end": "3070-10-27T21:00:00Z"
      }
    }
  }
]

所以这里的问题是b没有像预期的那样在中间,因为它考虑了区分大小写。不仅如此,该列表的顺序似乎也与预期顺序相反。

我在这里做错了什么,我怎样才能让它忽略区分大小写?

你的代码稍作改动(使用管道而不是 useWith,我希望尽可能避免),我们可以像这样在比较器中添加 toLower

const makeSorter = compose (
  sortWith,
  map (({order, name}) => (order == 'desc' ? descend : ascend) (
    compose (toLower, path (split ('.', name)))
  ))
)

const sorts = [
  { order: 'asc', name: 'name' },
  { order: 'desc', name: 'purchase.period.start' },
]

// Switched the order to make the sorting clear
const theList = [{name: "C", purchase: {period: {start: "2020-01-26T21: 00: 00Z", end: "3070-10-27T21: 00: 00Z"}}}, {name: "A", purchase: {period: {start: "2020-01-08T21: 00: 00Z", end: "3070-10-27T21: 00: 00Z"}}}, {name: "b", purchase: {period: {start: "2019-09-30T19: 00: 00Z", end: "3070-10-27T21: 00: 00Z"}}}]

console .log (
  makeSorter (sorts) (theList)
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
<script> const {compose, sortWith, map, ascend, descend, toLower, path, split} = R </script>

我还将测试从 asc 更改为 desc,因为我使用过类似的东西,其中 asc 是默认值,字段是可选的。 (desc 在需要时是强制性的。)

这只是避免了我们传递给 ascenddescend:

的函数中的一些重复
    (order == 'desc' ? descend : ascend)

我还没有测试过这是否适用于多种类型,因为这些值在 'name' 上都不一致,但它看起来应该有效。