按特定给定值对数组中的元素进行排序

时间:2019-12-18 05:57:15

标签: javascript arrays sorting arraylist

我收到的对象是这样的数组(有很多字段,我只显示少数几个字段):

dataset = [
{branchID : "IN123", customerID : "CB1234", customerName : "Alex", amountApplied : "1000.0"},
{branchID : "IN124", customerID : "CB1235", customerName : "Tom", amountApplied : "2000.0"},
{branchID : "IN128", customerID : "CB1236", customerName : "Adrian", amountApplied : "5000.0"}
]

我需要按此顺序查看字段的顺序

dataset = [
{branchID : "IN123", amountApplied : "1000.0", customerName : "Alex", customerID : "CB1234"}
{branchID : "IN124", amountApplied : "2000.0", customerName : "Tom" , customerID : "CB1235"}
{branchID : "IN128", amountApplied : "5000.0", customerName : "Adrian", customerID : "CB1236" }
]

我尝试过这个:

    var ordering = {},
          sortOrder = ['branchID',,'amountApplied','customerName','customerID']
         for (var i=0; i<sortOrder.length; i++)
         ordering[sortOrder[i]] = i;

            response.dataSet.sort( function(a, b) {
             return (ordering[a] - ordering[b]) || a.localeCompare(b);
         });

它显示.localCompare不是函数。 任何建议如何实现这一目标

0 个答案:

没有答案