-1

i have data like below,

const subItems = [
    {
        id: '1',
        title: 'subitem-one',
        status: 'new',
        createdAt: '2020-08-13T16:32:10.000Z',
        orders: [
            {
                id: '1',
                title: 'subitem1-order-one',
                status: 'new',
            },
            {
                id: '2',
                title: 'subitem1-order-two',
                status: 'new',
            },
        ]
    },

    {
        id: '2',
        title: 'subitem-two',
        status: 'new',
        createdAt: '2020-08-16T12:02:06.000Z',
        orders: [
            {
                id: '2',
                title: 'subitem2-order-one',
                status: 'new',
            },
       ],
    },
]

how can i sort the subItems array of objects in ascending order based on createdAt property using javascript.

expected output is as below,

const subItems = [
    {
        id: '1',
        title: 'subitem-one',
        status: 'new',
        createdAt: '2020-08-16T16:32:10.000Z',
        orders: [
            {
                id: '1',
                title: 'subitem1-order-one',
                status: 'new',
            },
            {
                id: '2',
                title: 'subitem1-order-two',
                status: 'new',
            },
        ]
    },

    {
        id: '2',
        title: 'subitem-two',
        status: 'new',
        createdAt: '2020-08-13T12:02:06.000Z',
        orders: [
            {
                id: '2',
                title: 'subitem2-order-one',
                status: 'new',
            },
        ],
    },
]

how can i do sort the data in ascending order based on createdAt. the most recently created should be first. could someone help me with this. thanks.

Domino987
  • 6,538
  • 2
  • 5
  • 28
saritha
  • 499
  • 1
  • 9

3 Answers3

-1

Ascending

subItems.sort((a,b)=>new Date(a.createdAt) - new Date(b.createdAt))

Descending

subItems.sort((a,b)=>new Date(b.createdAt) - new Date(a.createdAt))
Anup
  • 501
  • 3
  • 7
-1

Use the Array#sort function with a callback for your sort-algorithm.

function sortSubItems(subItems) {
    subItems.sort((a,b) => {
        a= (new Date(a.createdAt)).getTime();
        b= (new Date(b.createdAt)).getTime();
        return b-a;
    });
    
    return subItems;
}

const subItems = [
    {
        id: '1',
        title: 'subitem-one',
        status: 'new',
        createdAt: '2020-08-13T16:32:10.000Z',
        orders: [
            {
                id: '1',
                title: 'subitem1-order-one',
                status: 'new',
            },
            {
                id: '2',
                title: 'subitem1-order-two',
                status: 'new',
            },
        ]
    },
    {
        id: '2',
        title: 'subitem-two',
        status: 'new',
        createdAt: '2020-08-16T12:02:06.000Z',
        orders: [
            {
                id: '2',
                title: 'subitem2-order-one',
                status: 'new',
            }
        ]
    },
];

console.log(sortSubItems(subItems));
Sascha
  • 4,416
  • 3
  • 11
  • 33
-1

You sĀ“can simply use the sort function with the callback:

const sortedItems = subItems.sort((a,b) => b.createdAt.localeCompare(a.createdAt))
Domino987
  • 6,538
  • 2
  • 5
  • 28