Home > Mobile >  Sort data by time slot in javascript
Sort data by time slot in javascript

Time:04-27

I have to sort data coming from the server dynamically by differents fields. It is working perfectly with all the fields except by time slot field. I have to sort data by time slot field in ascedening order, the problem is that the time slot is string e.g: "09:00-09:59".

My sorting function is:

function sortArray(array: any[], direction: string, extractor: (any) => any): any[] {
  return array.sort((e1, e2) =>
    (direction === 'asc' && extractor(e1) > extractor(e2)) || (direction !== 'asc' && extractor(e1) < extractor(e2))
      ? 1
      : -1
  );
}

How can I fix it? If anyone know then please let me know. Thank you.

CodePudding user response:

Your sortArray function is fine. You have to check sort.active and sort.direction are updating correctly.

Try like this:

  console.log('this.sort.active', this.sort.active);
  console.log('this.sort.direction', this.sort.direction);

And If they are not updating then this is the problem. Then revise your logic of updating these things.

  • Related