1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

配列並び替え

Posted at
interface Item {
  itemA: number;
  itemB: string;
  itemC: string;
}

// 2つのオブジェクト配列を作成
const array1: Item[] = [
  { itemA: 2, itemB: 'b', itemC: 'z' },
  { itemA: 1, itemB: 'a', itemC: 'x' },
  { itemA: 2, itemB: 'a', itemC: 'y' },
];

const array2: Item[] = [
  { itemA: 1, itemB: 'b', itemC: 'y' },
  { itemA: 3, itemB: 'a', itemC: 'x' },
  { itemA: 1, itemB: 'a', itemC: 'z' },
];

// 配列を結合
const combinedArray: Item[] = [...array1, ...array2];

// itemA順、itemB順、itemC順に並べ替え
combinedArray.sort((a, b) => {
  if (a.itemA !== b.itemA) {
    return a.itemA - b.itemA;
  }
  if (a.itemB !== b.itemB) {
    return a.itemB.localeCompare(b.itemB);
  }
  return a.itemC.localeCompare(b.itemC);
});

console.log(combinedArray);
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?