How to sort object based on time (HH:MM)

Hey everyone, I have an object as below const arr =[ { id: 123, time: "10:58" }, { id: 727, time: "16:00" }, { id: 343, time: "10:12" }, ... ] I want to sort this array based on time, like whichever object has earlier time should be above than the object which has time later.
8 Replies
janglad
janglad•10mo ago
you can use .sort for this. You'll need to pass it a function that takes in 2 objects, both having a time key and then compares those https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
MDN Web Docs
Array.prototype.sort() - JavaScript | MDN
The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
janglad
janglad•10mo ago
note that this will modify your original array, if you want to just return a sorted array (and keep the original as is) us toSorted. Altho I don't think that's supported in Internet Explorer, should that matter
Noor
NoorOP•10mo ago
Yes I tried the sort function but problem is if two objects have same hours then I need to compare the minutes. How can I do that?
janglad
janglad•10mo ago
well you're kinda storing the time in a bit of an awkward way. But if you need to store them as strings, split the string using : as the separator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split then first compare the first item, if those are equal compare the second
MDN Web Docs
String.prototype.split() - JavaScript | MDN
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
janglad
janglad•10mo ago
altho actually you could probably also just filter out the : using .replace https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace replacing : with an empty string, then cast it to a number and compare those
MDN Web Docs
String.prototype.replace() - JavaScript | MDN
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.
janglad
janglad•10mo ago
so you compare both in one go sorry for all the links 😄 but also honestly this kinda stuff is something GPT is good at! Could be helpful to ask since you can also easily ask follow up questions
Noor
NoorOP•10mo ago
Great. I got the logic and I tried GPT too. It worked. Thanks you This is what the input type="time" provides so I got no choice to store like that 🥲
janglad
janglad•10mo ago
Awesome! happy to help 🙂 and aah yeah I see, makes sense!
Want results from more Discord servers?
Add your server