-3

let say I have array like following

[
    {
        id:"1",
        name: "satu",
        data: [
            {
                id: "1.a",
                last_update: "07:00:00",
                value: 12
            },
            {
                id: "1.b",
                last_update: "08:00:00",
                value: 0
            }
        ]
    },
    {
        id:"2",
        name: "dua",
        data: [
            {
                id: "2.a",
                last_update: "12:00:00",
                value: 1
            },
            {
                id: "2.a",
                last_update: "06:00:00",
                value: 5
            },
        ]
    },
    {
        id:"3",
        name: "tiga",
        data: [
            {
                id: "3.a",
                last_update: "09:00:00",
                value: 17
            },
            {
                id: "3.b",
                last_update: "10:00:00",
                value: 2
            }
        ]
    }
]

how can I sort this array based on last_update prop inside data array? I expected id=2 will be the first element of the sorted array, follow by id=3. the final result should be like following

[
    {
        id:"2",
        name: "dua",
        data: [
            {
                id: "2.a",
                last_update: "12:00:00",
                value: 1
            },
            {
                id: "2.a",
                last_update: "06:00:00",
                value: 5
            },
        ]
    },
    {
        id:"3",
        name: "tiga",
        data: [
            {
                id: "3.a",
                last_update: "09:00:00",
                value: 17
            },
            {
                id: "3.b",
                last_update: "10:00:00",
                value: 2
            }
        ]
    },
    {
        id:"1",
        name: "satu",
        data: [
            {
                id: "1.a",
                last_update: "07:00:00",
                value: 12
            },
            {
                id: "1.b",
                last_update: "08:00:00",
                value: 0
            }
        ]
    }
]
Dariel Pratama
  • 1,607
  • 3
  • 18
  • 49

2 Answers2

2

You can use Array.sort with your own sorting function

Here's a quick example

let objs = [
    {
        id:"1",
        name: "satu",
        data: [
            {
                id: "1.a",
                last_update: "07:00:00",
                value: 12
            },
            {
                id: "1.b",
                last_update: "08:00:00",
                value: 0
            }
        ]
    },
    {
        id:"2",
        name: "dua",
        data: [
            {
                id: "2.a",
                last_update: "12:00:00",
                value: 1
            },
            {
                id: "2.a",
                last_update: "06:00:00",
                value: 5
            },
        ]
    },
    {
        id:"3",
        name: "tiga",
        data: [
            {
                id: "3.a",
                last_update: "09:00:00",
                value: 17
            },
            {
                id: "3.b",
                last_update: "10:00:00",
                value: 2
            }
        ]
    }
]

function compare( a, b ) {
  if ( a.data[0].last_update < b.data[0].last_update ){
    return -1;
  }
  if ( a.data[0].last_update > b.data[0].last_update ){
    return 1;
  }
  return 0;
}

objs.sort( compare );

Looking at your comment, maybe you want to add more conditions?

Skelly
  • 109
  • 1
  • 10
0

I solved it, here is working solution.

first of sort the data array inside each object

data.sort((a, b) => Date.parse(b.last_update) - Date.parse(a.last_update))

then sort the main array

main.sort(a, b) => Date.parse(b.data[0].last_update) - Date.parse(b.data[0].last_update));

suppose every object has its data atleast 1 element.

in my actual code I am checking if the data array has atleast 1 element inside of it to prevent error undefined index 0

Dariel Pratama
  • 1,607
  • 3
  • 18
  • 49