0

I have an array of objects. I want to sort the array according to the object key. For example below -

{
   "id": 3511,
   "time": "03:30",
   "hour": 3,
   "utc_date_time": "2020-07-07T02:07:54.000Z",
   "members": 0
},
{
   "id": 3514,
   "time": "04:30",
   "hour": 4,
   "utc_date_time": "2020-07-07T02:07:54.000Z",
   "members": 0
},
{
   "id": 3513,
   "time": "04:00",
   "hour": 4,
   "utc_date_time": "2020-07-07T02:07:54.000Z",
   "members": 0
},

I want to sort it according to time like this-

{
   "id": 3511,
   "time": "03:30",
   "hour": 3,
   "utc_date_time": "2020-07-07T02:07:54.000Z",
   "members": 0
},
{
   "id": 3513,
   "time": "04:00",
   "hour": 4,
   "utc_date_time": "2020-07-07T02:07:54.000Z",
   "members": 0
},
{
   "id": 3514,
   "time": "04:30",
   "hour": 4,
   "utc_date_time": "2020-07-07T02:07:54.000Z",
   "members": 0
},

I have used this function to sort but it is not giving the expected output I have also used the time key, still the same.

timeSlots.sort(function(a, b) {
  return a.time- b.time;
});

But not getting an expected output.

8
  • how can i convert time key? i need it to be according to time key Commented Jul 6, 2020 at 11:59
  • Do you want to sort based on time or hour? Your question is regarding time but your sort in regarding hour Commented Jul 6, 2020 at 12:00
  • 1
    Does this answer your question? Sort Array Elements (string with numbers), natural sort Commented Jul 6, 2020 at 12:00
  • @Codenewbie Why parseFloat()? hour is already a number o.O Commented Jul 6, 2020 at 12:01
  • 1
    actually even if its a string javascript will coerce it Commented Jul 6, 2020 at 12:02

4 Answers 4

3

Since your .time values are in HH:MM format, you can sort them as strings:

let array = [{
    "id": 3511,
    "time": "03:30",
    "hour": 3,
    "utc_date_time": "2020-07-07T02:07:54.000Z",
    "members": 0
  },
  {
    "id": 3514,
    "time": "04:30",
    "hour": 4,
    "utc_date_time": "2020-07-07T02:07:54.000Z",
    "members": 0
  },
  {
    "id": 3513,
    "time": "04:00",
    "hour": 4,
    "utc_date_time": "2020-07-07T02:07:54.000Z",
    "members": 0
  }
];

array.sort((a, b) => a.time.localeCompare(b.time));
console.log(array);

Sign up to request clarification or add additional context in comments.

Comments

1

Lodash's "sortBy" is perfect for this.

_.sortBy(timeSlots, 'time');

Comments

0
let arr =    [{
        "id": 3511,
        "time": "03:30",
        "hour": 3,
        "utc_date_time": "2020-07-07T02:07:54.000Z",
        "members": 0
    },
    {
        "id": 3514,
        "time": "04:30",
        "hour": 4,
        "utc_date_time": "2020-07-07T02:07:54.000Z",
        "members": 0
    },
    {
        "id": 3513,
        "time": "04:00",
        "hour": 4,
        "utc_date_time": "2020-07-07T02:07:54.000Z",
        "members": 0
    }]
    

sortedArr = arr.sort(function(a, b){
                if (a['time'] < b['time']) return -1;
                if (a['time'] > b['time']) return 1;
                return 0
           })

console.log(`sorted: `, sortedArr);

Comments

0

Your can pass a compare function to sort method saying how you want to sort the array elements. Thus, it provides a way to sort objects too.

To sort in ascending order:

const sortedArrayOfObjects = array.sort((item1, item2) => {
    if(item1.id < item2.id) return -1
    else if(item1.id > item2.id) return 1
    else return 0;
});
  • If compareFunction(a, b) returns less than 0, sort a to an index lower than b (i.e. a comes first).
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.
  • If compareFunction(a, b) returns greater than 0, sort b to an index lower than a (i.e. b comes first).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.