0

I have a list which I want sort according to Arrival Date & Departure Date based on Caption and Value. But Arrival and Departure don't exist together in the list. If sort using arrival, then the departure can be ignored, just push to the bottom of the list, and vice versa. How can I achieve that?

var svehicleList = [
    {
        Brand: 'Volvo',
        Summary: [
            {
                Caption: "Consignee",
                Value: "ONE TO ONE SDN BHD",
            },
            {
                Caption: "Arrival",
                Value: "18-08-2020 06:01",
            }
        ]
    },
    {
        Brand: 'BMW',
        Summary: [
            {
                Caption: "Consignee",
                Value: "ABC SDN BHD",
            },
            {
                Caption: "Arrival",
                Value: "14-08-2020 16:03",
            }
        ]
    },
    {
        Brand: 'TOYOTA',
        Summary: [
            {
                Caption: "Consignee",
                Value: "MATTA SDN BHD",
            },
            {
                Caption: "Departure",
                Value: "14-08-2020 16:03",
            }
        ]
    },
    {
        Brand: 'FERRARI',
        Summary: [
            {
                Caption: "Consignee",
                Value: "GLOBAL P SDN BHD",
            },
            {
                Caption: "Departure",
                Value: "14-08-2020 18:03",
            }
        ]
    },
    {
        Brand: 'HONDA',
        Summary: [
            {
                Caption: "Consignee",
                Value: "HH SDN BHD",
            },
            {
                Caption: "Arrival",
                Value: "09-09-2020 16:03",
            }
        ]
    },
];

Edited: There is actually two object in summary.

Sheng Jie
  • 141
  • 3
  • 16
  • which direction do you want to sort? what have you tried? (why is summary an array?) – Nina Scholz Apr 14 '21 at 12:53
  • 1
    You need to create your own comparing function for [`sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). – Rojo Apr 14 '21 at 13:04

2 Answers2

0

This one helped me: How to Sort Multi-dimensional Array by Value?

You have to write your own comparision function.

sbrand5020
  • 66
  • 5
0

You could find the object with the wanted Caption, then sort by occurence and then by date with a wrapper for getting an ISO date.

const
    getISO = (string = '') => string.replace(/^(..)-(..)-(....) (.*$)/, '$3-$2-$1 $4'),    
    sortBy = caption => ({ Summary: a }, { Summary: b }) => {
        const
            aa = a.find(o => o.Caption === caption),
            bb = b.find(o => o.Caption === caption);

        return !aa - !bb
            || getISO(aa?.Value).localeCompare(getISO(bb?.Value));
    },
    vehicleList = [{ Brand: 'Volvo', Summary: [{ Caption: "Consignee", Value: "ONE TO ONE SDN BHD" }, { Caption: "Arrival", Value: "18-08-2020 06:01" }] }, { Brand: 'BMW', Summary: [{ Caption: "Consignee", Value: "ABC SDN BHD" }, { Caption: "Arrival", Value: "14-08-2020 16:03" }] }, { Brand: 'TOYOTA', Summary: [{ Caption: "Consignee", Value: "MATTA SDN BHD" }, { Caption: "Departure", Value: "14-08-2020 16:03" }] }, { Brand: 'FERRARI', Summary: [{ Caption: "Consignee", Value: "GLOBAL P SDN BHD" }, { Caption: "Departure", Value: "14-08-2020 18:03" }] }, { Brand: 'HONDA', Summary: [{ Caption: "Consignee", Value: "HH SDN BHD" }, { Caption: "Arrival", Value: "09-09-2020 16:03" }] }];

vehicleList.sort(sortBy('Arrival'));
console.log(vehicleList);

vehicleList.sort(sortBy('Departure'));
console.log(vehicleList);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hi, I have typo in Summary, Summary is an array of object actually. But currently your method is not working when Summary become an array. Any idea? – Sheng Jie Apr 15 '21 at 01:53