1

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 })));
Pruthvi Reddy
  • 47
  • 3
  • 9
  • What does the moment function? – Paflow Aug 21 '19 at 10:29
  • 2
    The problem with your code is that `parsedDates.get(b)` and `parsedDates.get(a)` return `undefined`. Since you can't sort a Map, first sort the `elements` array, then create a Map from it. –  Aug 21 '19 at 10:35
  • The array being passed to the new `Map` doesn't make much sense. What exactly are you trying to accomplish there? – charlietfl Aug 21 '19 at 10:41

3 Answers3

12

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; }
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

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' }
// ]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hero Qu
  • 911
  • 9
  • 10
0

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
Jothi Basu
  • 166
  • 6