简体   繁体   English

Javascript:基于时间戳属性对对象数组进行排序不会正确排序一个元素

[英]Javascript: Sorting an array of objects based on a timestamp property doesn't sort one element correctly

I am trying to sort an array which contains arrays of two elements of the form [[string, object], [string, object], ...]. 我正在尝试对包含[[string,object],[string,object],...]形式的两个元素的数组的数组进行排序。 Each object has a property named: currentStatusTimestamp. 每个对象都有一个名为currentStatusTimestamp的属性。 I am trying to sort the internal arrays based on this property. 我试图基于此属性对内部数组进行排序。 It seems that all the elements are sorted correctly except one element which stays always at the first position of the array. 看起来除了一个始终位于数组第一个位置的元素外,所有元素都被正确排序。

You can view an example describing the problem below. 您可以在下面查看描述问题的示例。

Unsorted: Unsorted array 未排序: 未排序的数组

Sorted: Sorted array 排序: 排序数组

The first element at each line of the images corresponds to arr[0] (string) and the second to the arr[1].currentStatusTimestamp . 图像每行的第一个元素对应于arr[0] (字符串),第二个元素对应于arr[1].currentStatusTimestamp

I have tried to sort the arrays with two different functions, but neither seem to work. 我试图用两个不同的函数对数组进行排序,但似乎都不起作用。

You may find the two functions below: 您可以在下面找到以下两个功能:

const sortByCurrentStatusTimestamp2 = (array) => {
array.sort((a,b) => {
    return (Number(a[1].currentStatusTimestamp) < Number(b[1].currentStatusTimestamp)) ? 1 : ((Number(b[1].currentStatusTimestamp) > Number(a[1].currentStatusTimestamp)) ? -1 : 0);} );}

const sortByCurrentStatusTimestamp = (array) => {
array.sort((a, b) => {
    return Number(a[1].currentStatusTimestamp) < Number(b[1].currentStatusTimestamp);
});}

The code which calls the functions is displayed below: 调用函数的代码如下所示:

sortByCurrentStatusTimestamp2(entriesArray);

or 要么

sortByCurrentStatusTimestamp(entriesArray);

You may find the code used to display the output which is shown in the images, below: 您可能会发现用于显示输出的代码,如下图所示:

for (let e of entriesArray) {                    
console.log(e[0],e[1].currentStatusTimestamp);}

sortByCurrentStatusTimestamp2(entriesArray);

for (let e of entriesArray) {
console.log(e[0],e[1].currentStatusTimestamp);}

The JS .sort function requires a comparator that returns a negative number, zero, or a positive number. JS .sort函数需要一个返回负数,零或正数的比较器。 Your second returns a boolean. 你的第二个返回一个布尔值。

Your first function fails because you do (a < b) ? 1 : (b > a) ? -1 : 0 你的第一个功能失败了,因为你做了(a < b) ? 1 : (b > a) ? -1 : 0 (a < b) ? 1 : (b > a) ? -1 : 0 (a < b) ? 1 : (b > a) ? -1 : 0 , but the two comparisons are equivalent, ie you got the second comparison the wrong way around. (a < b) ? 1 : (b > a) ? -1 : 0 ,但这两个比较是等价的,即你得到的第二个比较是错误的。

It normally suffices to just return the difference between the two numeric values: 通常只需返回两个数值之间的差异就足够了:

.sort((a, b) => +a[1].currentStatusTimestamp - +b[1].currentStatusTimestamp);

This will give the results in ascending order. 这将按升序给出结果。 Swap a and b to reverse that. 交换ab以反转它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM