排序功能无法正常工作

时间:2019-03-28 10:26:19

标签: javascript sorting

我在contactArray变量中有以下结果数组。在这里,我需要按特定记录的LastModifiedDate对以下记录进行排序。

在前端我有3条记录,第一次我只选择一条记录并在后端进行一些操作,然后按Asc顺序再次显示到前端。

(3) [{…}, {…}, {…}]
    0: {Id: "0034E00000nNLPfQAO", Name: "Test Contact 1"}
    1: {Id: "0034E00000nNLTbQAO", Name: "Test Contact 2", LastModifiedDate: "2019-03-28T09:53:00.000Z", Status__c: "Pending", Status_Reason__c: undefined}
    2: {Id: "0034E00000nP8VuQAK", Name: "Test Contact 3"}

使用下面的代码片段,sortedArray如下所示,

(3) [{…}, {…}, {…}]
    0: {Id: "0034E00000nNLTbQAO", Name: "Test Contact 2", LastModifiedDate: "2019-03-28T09:53:00.000Z", Status__c: "Pending", Status_Reason__c: undefined}
    1: {Id: "0034E00000nNLPfQAO", Name: "Test Contact 1"}
    2: {Id: "0034E00000nP8VuQAK", Name: "Test Contact 3"}

第二次尝试,我从前端选择了另一个取消选择记录,

(3) [{…}, {…}, {…}]
    0: {Id: "0034E00000nNLPfQAO", Name: "Test Contact 1", LastModifiedDate: "2019-03-28T09:59:13.000Z", Status__c: "Pending", Status_Reason__c: undefined}
    1: {Id: "0034E00000nNLTbQAO", Name: "Test Contact 2", LastModifiedDate: "2019-03-28T09:53:00.000Z", Status__c: "Pending", Status_Reason__c: undefined}
    2: {Id: "0034E00000nP8VuQAK", Name: "Test Contact 3"}

排序后的数组如下所示,

(3) [{…}, {…}, {…}]
    0: {Id: "0034E00000nNLPfQAO", Name: "Test Contact 1", LastModifiedDate: "2019-03-28T09:59:13.000Z", Status__c: "Pending", Status_Reason__c: undefined}
    1: {Id: "0034E00000nNLTbQAO", Name: "Test Contact 2", LastModifiedDate: "2019-03-28T09:53:00.000Z", Status__c: "Pending", Status_Reason__c: undefined}
    2: {Id: "0034E00000nP8VuQAK", Name: "Test Contact 3"} 

以下是我用来对数组进行排序的代码段,

var sortedContactArray = contactArray.sort(function(obj1, obj2) {
var tc1RefUndefined = obj1.LastModifiedDate == null ? 1 : 0;
var tc2RefUndefined = obj2.LastModifiedDate == null ? 1 : 0;

if (tc1RefUndefined || tc2RefUndefined) {
    return new Date(tc1RefUndefined) - new Date(tc2RefUndefined);
    }
});

我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

您可以将不具有LastModifiedDate属性(或任何falsy值)的项目移到底部,然后按字符串对ISO 8601日期进行排序。

var array = [{ Id: "0034E00000nNLPfQAO", Name: "Test Contact 1", LastModifiedDate: "2019-03-28T09:59:13.000Z", Status__c: "Pending", Status_Reason__c: undefined }, { Id: "0034E00000nNLTbQAO", Name: "Test Contact 2", LastModifiedDate: "2019-03-28T09:53:00.000Z", Status__c: "Pending", Status_Reason__c: undefined }, { Id: "0034E00000nP8VuQAK", Name: "Test Contact 3" }];

array.sort(({ LastModifiedDate: a }, { LastModifiedDate: b }) =>
     !a - !b || (a || '').localeCompare(b || ''));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

只需在您的排序条件中删除三元函数并直接使用LastModifiedDate进行比较即可。

var x = [{Id: "0034E00000nNLPfQAO", Name: "Test Contact 1", LastModifiedDate: "2019-03-28T09:59:13.000Z", Status__c: "Pending", Status_Reason__c: undefined},{Id: "0034E00000nNLTbQAO", Name: "Test Contact 2", LastModifiedDate: "2019-03-28T09:53:00.000Z", Status__c: "Pending", Status_Reason__c: undefined},{Id: "0034E00000nP8VuQAK", Name: "Test Contact 3"}];

var y = x.sort(function(obj1, obj2) {
var tc1RefUndefined = obj1.LastModifiedDate;   //<-- remove ternary
var tc2RefUndefined = obj2.LastModifiedDate;   //<-- remove ternary

if (tc1RefUndefined || tc2RefUndefined) {
    return new Date(tc1RefUndefined) - new Date(tc2RefUndefined);
    }
});

console.log(y)

之所以您的代码无法正常工作,是因为对象中存在“ LastModifiedDate”时,您正在将1分配给局部变量。

因此,当您为contactArray[0] and contactArray[1]运行排序功能时

var tc1RefUndefined = 0;//(result of ternary condition)
var tc2RefUndefined = 0;//(result of ternary condition)

if (tc1RefUndefined || tc2RefUndefined) { //this will not run due to false
    return new Date(tc1RefUndefined) - new Date(tc2RefUndefined);
    }
})
//so your array remain unaffected
相关问题