如何对 luxon 对象数组执行排序

sho*_*000 8 javascript sorting luxon

我正在尝试使用 js 类型的 luxon 时间对象。我不确定这是否如排序所期望的那样正确-1,0,1

const results  = objectGroupedByYearMonth[year][month].results.sort(
          (a,b) => 
            DateTime.fromISO(b.created)
              .diff(DateTime.fromISO(a.created))
        )
Run Code Online (Sandbox Code Playgroud)

这将返回一个 dt 对象console.log("DIF: ", DateTime.fromISO("2020-11-03T17:01:22.205041Z") .diff(DateTime.fromISO("2020-11-03T17:15:23.998284Z")))

Mar*_*tin 12

您可能希望将对象转换DateTime为数字 Unix 时间戳:

function compareLuxonDates(a: DateTime, b: DateTime) {
  return a.toMillis() - b.toMillis()
}
arrayWithDateObjects.sort(compareLuxonDates)
Run Code Online (Sandbox Code Playgroud)


小智 7

根据他们的文档,Luxon 的DateTime类实现了#valueof,它返回对象的原始值\xe2\x80\x93\xc2\xa0,在本例中它是纪元毫秒。这意味着您可以直接使用<或>比较 Luxon DateTime 对象。

\n

这是compareFn我有时用来对 Luxon DateTime 数组进行排序的单行代码。

\n

快速回答

\n
sortDateTimes = (a, b) => a < b ? -1 : a > b ? 1 : 0;\n
Run Code Online (Sandbox Code Playgroud)\n

这种方法之所以有效,是因为相等是包罗万象的条件(0最后的)。尽管您可以自由地<使用对象,但不能仅使用对象来检查相等性。Luxon为此提供了一个函数。或者,因为它们实现了,您可以使用以下命令将对象转换为其原始值>DateTime===.equals(...)#valueOfDateTime+。

\n
const { DateTime } = require(\'luxon\');\n\na = DateTime.fromISO(\'2020-01-15\');\nb = DateTime.fromISO(\'2020-01-15\');\n\na === b; // false\n+a === +b; // true\n
Run Code Online (Sandbox Code Playgroud)\n

上面的小compareFn一行代码在对象数组上工作得很好,但是由于我们经常想要对嵌入在属性中DateTime的对象数组进行排序(例如DateTimecreated(例如)进行排序,因此我们必须采取稍微不同的方法。

\n
sortByCreated = (a, b) =>  \n  a.created < b.created ? -1 : a.created > b.created ? 1 : 0;\n
Run Code Online (Sandbox Code Playgroud)\n

强有力的答案

\n

为了获得奖励积分,可以使用函数生成器使其更加可重用。

\n
createPropertySorter = (propertyName) => (a, b) =>\n  a[propertyName] < b[propertyName] ? -1 : a[propertyName] > b[propertyName] ? 1 : 0;\n
Run Code Online (Sandbox Code Playgroud)\n

当您需要排序时,将使用生成器而不是直接使用排序函数本身。从OP的例子来看:

\n
const results = objectGroupedByYearMonth[year][month].results.sort(\n    createPropertySorter(\'created\')\n  );\n\n// results is now sorted, e.g. results.map(JSON.stringify) is\n// [\n//  \'{"name":"some Object","created":"2020-01-25T00:00:00.000-07:00"}\',\n//  \'{"name":"some Object","created":"2020-01-31T00:00:00.000-07:00"}\',\n//  \'{"name":"some Object","created":"2020-02-01T00:00:00.000-07:00"}\',\n//  \'{"name":"some Object","created":"2020-02-01T00:00:00.000-07:00"}\',\n//  \'{"name":"some Object","created":"2020-02-07T00:00:00.000-07:00"}\'\n// ]\n
Run Code Online (Sandbox Code Playgroud)\n

注意可变性

\n

关于可变性的快速旁白:在上面的示例中,objectGroupedByYearMonth[year][month]有一个名为 的属性results。因为对sort()数组进行就地排序,objectGroupedByYearMonth[year][month].results所以也进行了排序(不仅仅是返回值)。这可能是OP的意图,或者可能无关紧要,但我认为值得关注。保留原始排序的修订版本将对副本进行排序使用扩展运算符对原始数组的

\n
const results = [...objectGroupedByYearMonth[year][month].results].sort(\n    createPropertySorter(\'created\')\n  );\n// results is sorted, while objectGroupedByYearMonth[year][month].results\n// is still in its original order\n
Run Code Online (Sandbox Code Playgroud)\n

逆向排序

\n

如果您想反转排序顺序(最新日期在前),请切换a并b在排序功能中

\n
createPropertyReverseSorter = (propertyName) => (a, b) =>\n  b[propertyName] < a[propertyName] ? -1 : b[propertyName] > a[propertyName] ? 1 : 0;\n
Run Code Online (Sandbox Code Playgroud)\n


Nin*_*olz 3

您可以直接将字符串与 进行比较String#localeCompare。

(a, b) => b.created.localeCompare(a.created)
Run Code Online (Sandbox Code Playgroud)

  • 因此,如果我想要升序或降序,我只需反转“b”和“a” (2认同)