Sort an object array by custom order

后端 未结 5 2292
名媛妹妹
名媛妹妹 2020-12-03 19:45

I have an array of objects which have a property called \'CODE\'.

[
  {
   ID: 168,
   NAME: \"First name\",
   CODE: \"AD\"
  },
  {
   ID: 167,
   NAME: \"         


        
相关标签:
5条回答
  • 2020-12-03 20:07

    You're going to use array.sort(customSort), where:

    function customSort(a,b)
    {
        a = item_order.indexOf(a.CODE);
        b = item_order.indexOf(b.CODE);
    
        return a - b;
    }
    
    0 讨论(0)
  • 2020-12-03 20:11

    You can use the function sort along with the function indexOf.

    var array = [  {   ID: 168,   NAME: "First name",   CODE: "AD"  },  {   ID: 167,   NAME: "Second name",   CODE: "CC"  },  {   ID: 169,   NAME: "Third name",   CODE: "CCM"  },  {   ID: 170,   NAME: "Fourth name",   CODE: "CR"  }],
        item_order = ["CCM","CR","AD","CC"];
    
    array.sort((a, b) => item_order.indexOf(a.CODE) - item_order.indexOf(b.CODE));
    
    console.log(array);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    0 讨论(0)
  • 2020-12-03 20:23

    If you have to do something like this often, you might write a small utility to help:

    const array = [{ ID: 168, NAME: "First name", CODE: "AD" }, { ID: 167, NAME: "Second name", CODE: "CC" }, { ID: 169, NAME: "Third name", CODE: "CCM" }, { ID: 170, NAME: "Fourth name", CODE: "CR" },{ ID: 166, NAME: "Fifth name", CODE: "CCM" }, { ID: 171, NAME: "Sixth name", CODE: "XXX" }, { ID: 172, NAME: "Seventh name", CODE: "CR" }]
    
    const sortOn = (prop, list) => {
      const order = list.reduce((obj, key, idx) => Object.assign(obj, { [key]: idx + 1}), {});
      const getVal = item => order[item[prop]] || Infinity
      
      return (a, b) => getVal(a) - getVal(b)
    }
    
    array.sort(sortOn('CODE', ["CCM", "CR", "AD", "CC"]))
    console.log(array)

    The order object is much like what Nina Scholz suggested. The reason for idx + 1 rather than just idx is to simplify the next line. That line uses Infinity as a way to sort to the end those whose key value is either undefined or not in the sort list. If you want them at the beginning, you can use 0 or -Infinity.

    0 讨论(0)
  • 2020-12-03 20:26

    For huge arrays, I suggest to use an object for the indices.

    var array = [{ ID: 168, NAME: "First name", CODE: "AD" }, { ID: 167, NAME: "Second name", CODE: "CC" }, { ID: 169, NAME: "Third name", CODE: "CCM" }, { ID: 170, NAME: "Fourth name", CODE: "CR" }],
        item_order = ["CCM", "CR", "AD", "CC"],
        order = item_order.reduce((r, k, v) => Object.assign(r, { [k]: v }), {});
    
    array.sort((a, b) => order[a.CODE] - order[b.CODE]);
    
    console.log(array);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    0 讨论(0)
  • 2020-12-03 20:29

    var array = [
      {
       ID: 168,
       NAME: "First name",
       CODE: "AD"
      },
      {
       ID: 167,
       NAME: "Second name",
       CODE: "CC"
      },
      {
       ID: 169,
       NAME: "Third name",
       CODE: "CCM"
      },
      {
       ID: 170,
       NAME: "Fourth name",
       CODE: "CR"
      },
    ];
    
    var sortOrder =  ["CCM","CR","AD","CC"];
    
    var sorted = array.sort((a, b) => sortOrder.indexOf(a.CODE) - sortOrder.indexOf(a.CODE));
    
    console.log(sorted);

    0 讨论(0)
提交回复
热议问题