Pruthvi Reddy
Pruthvi Reddy

Reputation: 47

Array Sort by time hh:mm:ss

I am trying to sort the time. but I am unable to sort by time (hh:mm:ss) format. so i have used moments js. my array sort by time not get sorted. how sort array by using maps

I have an array of objects:

let elements =[
  {
    "id": 1,
    "date": "02:01:02"
  },
  {
    "id": 2,
    "date": "01:01:01"
  },
  {
    "id": 3,
    "date": "03:01:01"
  },
  {
    "id": 4,
    "date": "04:01:01"
  }
 ]; 


 let parsedDates = new Map(
        elements.map(e =>[["id", "date"],[e.id, moment(e.date, 'hh:mm:ss')]])
        );

    elements.sort((a, b) => parsedDates.get(a) - parsedDates.get(b));

    console.log(elements.map(e => ({ id: e.id, date: e.date })));

Upvotes: 1

Views: 4860

Answers (3)

Hero Qu
Hero Qu

Reputation: 951

The parsedDates map you've created is looking like:

Map {
  [ 'id', 'date' ] => [ 1, <some Date object> ],
  [ 'id', 'date' ] => [ 2, <some Date object> ],
  [ 'id', 'date' ] => [ 3, <some Date object> ],
  [ 'id', 'date' ] => [ 4, <some Date object> ]
}

And then you try to extract from it with elements like this:

parsedDates.get({ "id": 1, "date": "02:01:02" })

This should not work, because the key in a Map is and Array instance.

Even if you were using an array as a key:

parsedDates.get([ 1, "02:01:02" ])

this still wouldn't work, as this would be a different Object reference. I mean two arrays

a = [ 1, "02:01:02" ]
b = [ 1, "02:01:02" ]

are stored in different places and are different Objects, even though their values are identical.

So, you can modify your solution a bit:

let elements =[
  {
    "id": 1,
    "date": "02:01:02"
  },
  {
    "id": 2,
    "date": "01:01:01"
  },
  {
    "id": 3,
    "date": "03:01:01"
  },
  {
    "id": 4,
    "date": "04:01:01"
  }
 ]; 

let parsedDates = new Map(
      elements.map(e => [e.date, e])
      );

elements = elements.map(x => x.date).sort().map(x => parsedDates.get(x))

console.log(elements)

// [
//   { id: 2, date: '01:01:01' },
//   { id: 1, date: '02:01:02' },
//   { id: 3, date: '03:01:01' },
//   { id: 4, date: '04:01:01' }
// ]

Upvotes: 0

Jothi Basu
Jothi Basu

Reputation: 176

You can try this

function convertDateObj(hhmmss){
	let obj = new Date();//creates a Date Object using the clients current time

    let [hours,minutes,seconds] = hhmmss.split(':'); 

    obj.setHours(+hours); // set the hours, using implicit type coercion
    obj.setMinutes(minutes); //you can pass Number or String, it doesn't really matter
    obj.setSeconds(seconds);
    return obj;
}

let elements =[
  {
    "id": 1,
    "date": "02:01:02"
  },
  {
    "id": 2,
    "date": "01:01:01"
  },
  {
    "id": 3,
    "date": "03:01:01"
  },
  {
    "id": 4,
    "date": "04:01:01"
  }
 ]; 

elements.sort((a, b) => convertDateObj(a.date) - convertDateObj(b.date)); // Ascending order

elements.sort((a, b) => convertDateObj(b.date) - convertDateObj(a.date)); // Descending order

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22534

You can lexicographical sort the time using string.localeCompare().

let times = [ { "id": 1, "date": "02:01:02" }, { "id": 2, "date": "01:01:01" }, { "id": 3, "date": "03:01:01" }, { "id": 4, "date": "04:01:01" } ];
times.sort((a,b) => a.date.localeCompare(b.date));
console.log(times);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 12

Related Questions