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);
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme