0

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?

2
  • Why not just sort on the back end wherever you perform your query?
    – Adrian Larson
    Mar 28, 2019 at 13:29
  • It's not possible in my scenario, because I have sorted the Inner query record and pass the whole record but need the sorting according to the inner record LastModifiedDate. Mar 28, 2019 at 14:14

1 Answer 1

2

You need to return a value every time, and it needs to be numerical. Further, you're not actually comparing the dates to each other, so you'll get bad results; if both records have a date, you get a return value of 0, meaning they are "equal" to each other, which is clearly not what you intended. Instead, consider the following code:

var sortedContactArray = contactArray.sort(function(obj1, obj2) {
  return (new Date(obj1.LastModifiedDate || 0)) - 
         (new Date(obj2.LastModifiedDate || 0));
});

The OR operator (||) returns the left value if it exists, or the right value otherwise. In other words, we use either the LastModifiedDate field if it exists, or use Jan 1, 1970 otherwise, so all records without a date end up first in the list.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .