I’ve been looking at a lot of examples where people use the .sort() function in jQuery.
So for example:
$('#myId').sort(..);
By I cannot find any documentation for sort() in the jQuery API, can anyone show me its usage?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because it’s not part of jQuery (officially), but is a proxied Array.sort.
As Derek points out,
jQuery(...)does not return an array. Rather, jQuery adds a proxy to make the jQuery object “act like an array”:This proxy works because the
thisin a function is determined by the object on which the function was invoked. And, furthermore,Array.sort(andArray.splice) work on anythisthat is “array like” (has alengthand presumably properties0..length-1). Here is an example of a custom object [ab]usingArray.sort:YMMV following the “For internal use only” notes.