how to sort the nested array of objects in angular

  angular

im using nzzorro and angular for the ui .i need to sort the column in a table, im able to do it for the array of objects ,but got struck in nested array of objects.
I tried a way but it still showing undefined.

export interface ItemListData{
  itemCode:string;
  itemName:string;
  itemCategoryName:any;
  sizeRate:string;
  color:string;
  specification:string;
  itemVendor:any;

}

this is the interface iam using for the item list where item vendor contains vendor style code,supplier,manufacturer. i tried to apply sort for those nested array object, it is giving undefined

{
        name: 'Vendor Style Code',
        value: 'vendorStyleCode',
        sortFn: (a: ItemListData, b: ItemListData) =>(a?.itemVendor.forEach((element:any) => {
            element.vendorStyleCode ??"" })).localeCompare(b?.itemVendor.forEach((element:any) => {
                element.vendorStyleCode ??""})),
        filter: true,
        filterName: 'vendorStyleCode',
    }, {
        name: 'Vendor',
        value: 'vendor',
        // sortFn: (a: ItemListData, b: ItemListData) => a.itemVendor[0].supplierName.localeCompare(b.itemVendor[0].supplierName),
        filter: true,
        filterName: 'vendor',
    }, {
        name: 'Manufacture',
        value: 'manufacture',
        // sortFn: (a: ItemListData, b: ItemListData) => a.itemVendor[0].manufacturerName.localeCompare(b.itemVendor[0].manufacturerName),
        filter: true,
        filterName: 'manufacture',
    },

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.