Quick Sortfunction Array By Specific Value
Full Code at https://codepen.io/CodeLegend27/pen/ExPZRxa did read similary articles but didnt managed to solve it so my array looks like this: I have this loop for (let i = 0; i
Solution 1:
When you are saving the dates, save them using toJSON methond on the Date Object. Pass the sort function to the array sort function.
var array = [
{'name':'test1','date':'2020-06-19T14:21:31.633Z'},
{'name':'test2','date':'2020-07-30T12:22:53.113Z'},
]
functionsortDate(rec1,rec2){
returnnewDate(rec1.date) < newDate(rec2.date) ? -1 : 1
}
array.sort(sortDate)
console.log(array)
This is will sort the array in ascending order. If you want to sort it in descending order, either change the comparison sign to '>' or swap -1 and 1.
If the array internal is storing date field as dates data type, you can do the following
functionsortDate(rec1,rec2){
return rec1.date < rec2.date ? -1 : 1
}
ultraarr.sort(sortDate)
Post a Comment for "Quick Sortfunction Array By Specific Value"