Ashish Bairwa
Ashish Bairwa

Reputation: 775

Sorting an array of arrays according to a certain value within an object

I have an output from a function like, It basically contains an Id and value having an array of arrays. Here it goes:

[
  {
    "centre": "5f8a8da57cb24b64b69c430c",
    "value": [
      [
        "TOTAL",
        4
      ],
      [
        "A",
        1
      ],
      [
        "B",
        1
      ],
      [
        "C",
        0
      ],
      [
        "D",
        1
      ],
      [
        "E",
        1
      ]
    ]
  },
  {
    "centre": "1cbafa99d8c77251bea30f11",
    "value": [
      [
        "TOTAL",
        4
      ],
      [
        "A",
        2
      ],
      [
        "B",
        1
      ],
      [
        "C",
        1
      ],
      [
        "D",
        0
      ],
      [
        "E",
        0
      ]
    ]
  }
]

I want to sort all the values according to the second value of array in the descending manner and if the values are matching then sorting it according to alphabetical order from the first value of array. I'm not sure how to do such sort of sorting which comes under in an object having array of arrays.

Desired output:

[
  {
    "centre": "5f8a8da57cb24b64b69c430c",
    "value": [
      [
        "TOTAL",
        4
      ],
      [
        "A",
        1
      ],
      [
        "B",
        1
      ],
      [
        "D",
        1
      ],
      [
        "E",
        1
      ],
      [
        "C",
        0
      ]
    ]
  },
  {
    "centre": "1cbafa99d8c77251bea30f11",
    "value": [
      [
        "TOTAL",
        4
      ],
      [
        "A",
        2
      ],
      [
        "B",
        1
      ],
      [
        "C",
        1
      ],
      [
        "D",
        0
      ],
      [
        "E",
        0
      ]
    ]
  }
]

Upvotes: 0

Views: 84

Answers (4)

Ketan Ramteke
Ketan Ramteke

Reputation: 10675

let data = [
  {
centre: "5f8a8da57cb24b64b69c430c",
value: [["TOTAL", 4],["A", 1],["B", 1],["C", 0],["D", 1],["E", 1],],
  },
  {
centre: "1cbafa99d8c77251bea30f11",
value: [["TOTAL", 4],["A", 1],["G", 1],["K", 0],["D", 1],["B", 1],],
  },
];

let sol = data.map((d) => {
  d.value.sort((a,b) => a[0].localeCompare(b[0]));
  d.value.sort((a, b) => parseInt(b[1] - a[1]));
  return d;
});


console.log(sol);

Upvotes: 1

Alan Omar
Alan Omar

Reputation: 4227

let arr=[{centre:"5f8a8da57cb24b64b69c430c",value:[["TOTAL",4],["A",1],["B",1],["C",0],["D",1],["E",1]]},{centre:"1cbafa99d8c77251bea30f11",value:[["TOTAL",4],["A",2],["B",1],["C",1],["D",0],["E",0]]}];

arr.forEach((e) => {
    e.value.sort((f,s) => {
        if(s[1] === f[1])
            return f[0].codePointAt(0) - s[0].codePointAt(0) 
        return s[1]-f[1]}
)
})

console.log(arr)

Upvotes: 1

z1ne2wo
z1ne2wo

Reputation: 278

If numbers are not equal, will sort relative to them, otherwise relative to strings:

function numComparator(a, b) {
    return b[1] - a[1];
}

function strComparator(a, b) {
    return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0;
}

items = [["E",0],["A",2],["B",1],["C",1],["TOTAL",4],["D",0]];
items.sort((a, b) => a[1] === b[1] ? strComparator(a, b) : numComparator(a, b));

Thus, items are sorted relative to numbers, and items with the same numbers are additionally sorted by strings.

Also note that sort is modifying an original array, not creating a new array!

Upvotes: 1

michael
michael

Reputation: 4173

You can use Javascript sort function.

const arr = [
  {
    centre: '5f8a8da57cb24b64b69c430c',
    value: [['TOTAL', 4], ['A', 1], ['B', 1], ['C', 0], ['D', 1], ['E', 1]],
  },
  {
    centre: '1cbafa99d8c77251bea30f11',
    value: [['TOTAL', 4], ['A', 2], ['B', 1], ['C', 1], ['D', 0], ['E', 0]],
  },
];

const res = arr.map(item => {
  const {value, centre} = item;
  value.sort((a, b) => {
    if (a[1] === b[1]) {
      return b[0] - a[0];
    } else {
      return b[1] - a[1];
    }
  });
  return {centre, value};
});

console.log(res);

Upvotes: 1

Related Questions