I have the following Array of results in contactArray attribute. Here What I need is I'll need to sort the following records by LastModifiedDate of a particular record.
In the front end I have 3 records and First time I select only one record and do some manipulation in the back end and show again to the front end by Asc order.
(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"}
using the below code snippet the sortedArray was like below,
(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"}
Second attempt I have select another unselect record from the front-end,
(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"}
and the sorted array was like below,
(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"}
The following is the code snippet I have used to sort the array,
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);
}
});
Did I missed anything?