How to sort the following array of objects, place the weekday in the same piece, and in ascending order of time:
[
{"Start": "14:01", "End": "16:00", "Weekday": 1}
{"Start": "10:00am", "End": "12:00pm", "weekday": 0},
{"Start": "14:01", "End": "16:00", "weekday": 0},
{"Start": "10:00am", "End": "12:00pm", "Weekday": 1},
{"Start": "12:01", "End": "", "" Weekday ": 1},
{"Start": "12:01", "End": "", "" Weekday ": 0},
]
jquery provides a way to sort by: sort (). But this is only for primitive types such as String int. Not for objects, arrays of objects.
For objects, an array of objects we can use a custom method of sort ():
Array. Sort (
function Sortrule (A, b) {
if (A. Properties > B. properties) return 1 swap location
if (A. Properties < B. properties) return-1 do not swap positions
if (A. property = B. property) return 0 do not swap positions
}
)
Code: It is ok to sort it two times, first sort by time, in accordance with weekday. (but not in the first order according to the time weekday)
var d = [ {"Start": "10:00am", "End": "Weekday", "" ": 0}, {"Start": "12:01", "End": "Weekday": 0}, {"Start": "12:01", "End": "Weekday": 1}, {"Start": "10:00am", "End": "Weekday", "" ": 1}, {"Start": "14:01", "End": "16:00", "Weekday": 1}, {"Start": "14:01", "End": "16:00", "weekday": 0} ] ; D.sort (function (b) {if(A.start>b.start)return1 ; if(A.start<b.start)return-1 ; return0 ; }) ; D.sort (function (b) {if(A.weekday>b.weekday)return1 ; if(A.weekday<b.weekday)return-1 ; return0 ; })
jquery Object Array Sorting