根据另一个属性的升序对排序的对象数组进行排序

时间:2022-02-12 22:30:23

I have an array of objects having attributes TechType and ProductName. The given array is already sorted by TechType (not necessarily alphabetically); now within this sorted array, it has to be further sorted by ascending order based on ProductName.

我有一个具有TechType和ProductName属性的对象数组。给定的数组已经按TechType排序(不一定按字母顺序);现在在这个排序的数组中,它必须按照ProductName的升序进一步排序。

var products= [
    {
        "TechType": "ADSL",
        "ProductName": " Zen ADSL Services",
            }, {
        "TechType": "ADSL",
        "ProductName": "ADSL Services",
            }, {
        "TechType": "T1",
        "ProductName": "T1-Voice",
},{
      "TechType": "T1",
        "ProductName": " Aviate T1-Voice",


 }
];

The sorted array should be

排序的数组应该是

  var products= [
        {
            "TechType": "ADSL",
            "ProductName": " ADSL Services",
                }, {
            "TechType": "ADSL",
            "ProductName": "Zen ADSL Services",
                }, {
            "TechType": "T1",
            "ProductName": " Aviate T1-Voice",
    },{
          "TechType": "T1",
            "ProductName": " T1-Voice",


     }
    ];

1 个解决方案

#1


2  

This is somewhat related to stable sort. The typical way to ensure a stable sort is by adding auxiliary data by which should be sorted in case items are found to be the same.

这与稳定排序有些相关。确保稳定排序的典型方法是添加辅助数据,以便在发现项目相同时对其进行排序。

I'm doing this here using two map operations, similar to what you would use for a Schwartzian Transform; the auxiliary data is used only if the tech types don't match between two items.

我在这里使用两个map操作,类似于你用于Schwartzian变换的操作;仅当两个项目之间的技术类型不匹配时才使用辅助数据。

To demonstrate the correct behaviour I've moved the items around so that tech types are ordered in reverse order from the question.

为了证明正确的行为,我移动了项目,以便从问题中按相反的顺序排序技术类型。

var products = [{
  "TechType": "T1",
  "ProductName": "T1-Voice",
},{
  "TechType": "T1",
  "ProductName": "Aviate T1-Voice",
}, {
  "TechType": "ADSL",
  "ProductName": "Zen ADSL Services",
}, {
  "TechType": "ADSL",
  "ProductName": "ADSL Services",
}];

function sortByStableProperty(array, prop, fn)
{
  // decorate
  var temp = array.map(function(item, index) {
    return [item, index];
  });
  
  temp.sort(function(a, b) {
    // sort by auxiliary data or callback function
    return a[0][prop] == b[0][prop] ? fn(a[0], b[0]) : a[1] - b[1];
  });
  
  // undecorate
  return temp.map(function(item) {
    return item[0];
  });
}

// actual sort
products = sortByStableProperty(products, 'TechType', function(a, b) {
  return a.ProductName.localeCompare(b.ProductName);
});

console.log(JSON.stringify(products));

#1


2  

This is somewhat related to stable sort. The typical way to ensure a stable sort is by adding auxiliary data by which should be sorted in case items are found to be the same.

这与稳定排序有些相关。确保稳定排序的典型方法是添加辅助数据,以便在发现项目相同时对其进行排序。

I'm doing this here using two map operations, similar to what you would use for a Schwartzian Transform; the auxiliary data is used only if the tech types don't match between two items.

我在这里使用两个map操作,类似于你用于Schwartzian变换的操作;仅当两个项目之间的技术类型不匹配时才使用辅助数据。

To demonstrate the correct behaviour I've moved the items around so that tech types are ordered in reverse order from the question.

为了证明正确的行为,我移动了项目,以便从问题中按相反的顺序排序技术类型。

var products = [{
  "TechType": "T1",
  "ProductName": "T1-Voice",
},{
  "TechType": "T1",
  "ProductName": "Aviate T1-Voice",
}, {
  "TechType": "ADSL",
  "ProductName": "Zen ADSL Services",
}, {
  "TechType": "ADSL",
  "ProductName": "ADSL Services",
}];

function sortByStableProperty(array, prop, fn)
{
  // decorate
  var temp = array.map(function(item, index) {
    return [item, index];
  });
  
  temp.sort(function(a, b) {
    // sort by auxiliary data or callback function
    return a[0][prop] == b[0][prop] ? fn(a[0], b[0]) : a[1] - b[1];
  });
  
  // undecorate
  return temp.map(function(item) {
    return item[0];
  });
}

// actual sort
products = sortByStableProperty(products, 'TechType', function(a, b) {
  return a.ProductName.localeCompare(b.ProductName);
});

console.log(JSON.stringify(products));