jquery Object Array Sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.