如何在javascript(任何属性)中对对象数组进行排序?

时间:2015-10-28 06:18:54

标签: javascript jquery

我正在尝试使用此链接对对象数组进行排序 Sort array of objects by string property value in JavaScript

Sorting an array of JavaScript objects

但是当我尝试排序时,这里没有给出预期的结果是我的代码 https://jsfiddle.net/36wp6b98/

预期出局就是这个

[
    {
        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"
    }

]

代码

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)

0 个答案:

没有答案