0

I am trying to sort array of object using this link Sort array of objects by string property value in JavaScript

Sorting an array of JavaScript objects

and but when I try to sort it is not giving the expected result here is my code https://jsfiddle.net/36wp6b98/

Expected out put is this

[
    {
        Name: "1009834",
        Account__r: "Ad Imperial Bank",
        PO__c: "PO9957",
        Dispute__c: "-",
        Amount__c: "8182.62",
        Expected_Payment_Date__c: "Tue May 05 2015",
        Disputed_Amount__c: "0.00",
        Promised_Amount__c: "9006.80",
        Promise_Date__c: "Wed Jul 02 2014",
        Balance__c: "8182.62",
        Close_Date__c: "-",
        id: "a0RK0000002l366MAA"
    },
    {
        Name: "1009092",
        Account__r: "Canadian Imperial Bank",
        PO__c: "PO9215",
        Dispute__c: "-",
        Amount__c: "9370.98",
        Expected_Payment_Date__c: "Fri Jul 31 2015",
        Disputed_Amount__c: "0.00",
        Promised_Amount__c: "8695.90",
        Promise_Date__c: "Thu Mar 06 2014",
        Balance__c: "9370.98",
        Close_Date__c: "-",
        id: "a0RK0000002l365MAA"
    },
    {
        Name: "1008082",
        Account__r: "pacd Imperial Bank",
        PO__c: "PO8205",
        Dispute__c: "-",
        Amount__c: "4696.52",
        Expected_Payment_Date__c: "Tue May 05 2015",
        Disputed_Amount__c: "0.00",
        Promised_Amount__c: "0.00",
        Promise_Date__c: "-",
        Balance__c: "4696.52",
        Close_Date__c: "-",
        id: "a0RK0000002l364MAA"
    }

]

Code

function compare(a,b) {
  if (a.last_nom < b.last_nom)
    return -1;
  if (a.last_nom > b.last_nom)
    return 1;
  return 0;
}
var b=obj.sort(function(a, b) {
    return a.Account__r - b.Account__r;
})
console.log(b)
7
  • why are you not using the compare function after making it ? Commented Oct 28, 2015 at 6:20
  • 1
    Many duplicate answers for how to sort an array of objects by a property. Commented Oct 28, 2015 at 6:21
  • I used that function but it s not returning anything ? Commented Oct 28, 2015 at 6:29
  • @HarshalCarpenter could you please give fiddle Commented Oct 28, 2015 at 6:30
  • Please change my fiddle jsfiddle.net/36wp6b98 Commented Oct 28, 2015 at 6:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.