Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Sắp xếp mảng đối tượng theo nhiều thuộc tính trong JavaScript


Giả sử chúng ta có một mảng đối tượng như sau:

const arr = [
  { id: 1, score: 1, isCut: false, dnf: false },
  { id: 2, score: 2, isCut: false, dnf: false },
  { id: 3, score: 3, isCut: false, dnf: false },
  { id: 4, score: 4, isCut: false, dnf: false },
  { id: 5, score: 5, isCut: true, dnf: true },
  { id: 6, score: 6, isCut: true, dnf: false },
  { id: 7, score: 7, isCut: true, dnf: false },
  { id: 8, score: 8, isCut: true, dnf: false },
  { id: 9, score: 9, isCut: true, dnf: false },
  { id: 10, score: 0, isCut: false, dnf: false },
  { id: 11, score: -1, isCut: false, dnf: false },
  { id: 12, score: -2, isCut: false, dnf: true },
  { id: 13, score: -3, isCut: false, dnf: false },
  { id: 14, score: -4, isCut: false, dnf: false },
  { id: 15, score: -5, isCut: false, dnf: false },
  { id: 16, score: 10, isCut: true, dnf: false }
];

Chúng ta cần sắp xếp mảng trên theo các tiêu chí sau:

Nếu dnf là true,

đối tượng đi xuống cuối; tất cả đối tượng dnf nên được sắp xếp theo score

Nếu isCut là true,

đối tượng đi xuống cuối, nhưng ở trên dnf; tất cả đối tượng isCut nên được sắp xếp theo score

Phần còn lại nên được sắp xếp theo score, và nếu các score bằng nhau, thì theo id.

Ví dụ

Mã thực hiện:

const arr = [
  { id: 1, score: 1, isCut: false, dnf: false },
  { id: 2, score: 2, isCut: false, dnf: false },
  { id: 3, score: 3, isCut: false, dnf: false },
  { id: 4, score: 4, isCut: false, dnf: false },
  { id: 5, score: 5, isCut: true, dnf: true },
  { id: 6, score: 6, isCut: true, dnf: false },
  { id: 7, score: 7, isCut: true, dnf: false },
  { id: 8, score: 8, isCut: true, dnf: false },
  { id: 9, score: 9, isCut: true, dnf: false },
  { id: 10, score: 0, isCut: false, dnf: false },
  { id: 11, score: -1, isCut: false, dnf: false },
  { id: 12, score: -2, isCut: false, dnf: true },
  { id: 13, score: -3, isCut: false, dnf: false },
  { id: 14, score: -4, isCut: false, dnf: false },
  { id: 15, score: -5, isCut: false, dnf: false },
  { id: 16, score: 10, isCut: true, dnf: false }
];
const sortComplex = (arr = []) => {
  arr.sort(function (a, b) {
    const order = (dnf, isCut) => {
      return [0, 1, 3, 2][dnf * 2 + isCut];
    }
    return order(a.dnf, a.isCut) - order(b.dnf, b.isCut) || b.score - a.score;
  });
};
sortComplex(arr);
console.log(arr);

Kết quả

Kết quả trong console:

[
  { id: 4, score: 4, isCut: false, dnf: false },
  { id: 3, score: 3, isCut: false, dnf: false },
  { id: 2, score: 2, isCut: false, dnf: false },
  { id: 1, score: 1, isCut: false, dnf: false },
  { id: 10, score: 0, isCut: false, dnf: false },
  { id: 11, score: -1, isCut: false, dnf: false },
  { id: 13, score: -3, isCut: false, dnf: false },
  { id: 14, score: -4, isCut: false, dnf: false },
  { id: 15, score: -5, isCut: false, dnf: false },
  { id: 16, score: 10, isCut: true, dnf: false },
  { id: 9, score: 9, isCut: true, dnf: false },
  { id: 8, score: 8, isCut: true, dnf: false },
  { id: 7, score: 7, isCut: true, dnf: false },
  { id: 6, score: 6, isCut: true, dnf: false },
  { id: 5, score: 5, isCut: true, dnf: true },
  { id: 12, score: -2, isCut: false, dnf: true }
]