Summary of JavaScript sort () method

Source: Internet
Author: User
The sort () method is used to sort the elements of an array. Syntax: Arrayobject.sort (sortby), parameter SortBy Optional. Specify the sort order. Must be a function. Note: If the method is invoked without parameters, the elements in the array are sorted alphabetically, and more precisely, sorted in the order of character encoding. To do this, you should first convert the elements of the array into strings (if necessary) for comparison. Sort () Sorts the array, does not open new memory, and swaps the original elements. If you want to sort by other criteria, you need to provide a comparison function that compares two values, and then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B, and the return value would be as follows: If A is less than B, a value less than 0 is returned before a in the sorted array, and a should appear before B. If a equals B, it returns 0. If a is greater than B, a value greater than 0 is returned. The instance code is as follows:
var arrsimple2=new Array (1,8,7,6);
Arrsimple2.sort (function (a,b) {return a-b});
            Console.log (ArrSimple2)//[1, 6, 7, 8] var arr_data = [{' Icon ': ', ' ID ': 242,
            ' MenuLevel ': 3, ' menuName ': ' Travel orders ', ' menutop ': 1, ' menuurl ': '/', ' Buttoncontrol ': ' 0 ', ' supmenuid ': 241}, {' Icon ': ', ' ID ': 243
            , ' MenuLevel ': 3, ' menuName ': ' Visa order ', ' Menutop ': 3, ' menuurl ': '/', ' Buttoncontrol ': ' 0 ', ' supmenuid ': 241}, {' Icon ': ', ' ID '
            : 244, ' MenuLevel ': 3, ' menuName ': ' Notice of Regiment ', ' Menutop ': 2, ' menuurl ': '/', ' Buttoncontrol ': ' 0 ', ' supmenuid ': 241}]//Sort to menutop from small to large arr_data.sort (fu Nction (a,b) {return a.menutop-b.menutop}) Console.log (Arr_daTA)//[{icon: ',//id:242,//Menulevel:3,//menuName: ' Travel orders ',//menutop:1,//Menuurl: '/', Buttoncontrol: ' 0 ',//supmenuid:241},//{icon: ',//id:244,//Menulevel:3,//MenuName      : ' Group notice ',//Menutop:2,//Menuurl: '/',//Buttoncontrol: ' 0 ',//supmenuid:241},//{icon: ',// id:243,//Menulevel:3,//menuName: ' Visa orders ',//Menutop:3,//Menuurl: '/',//Buttoncontrol: ' 0 ',//supmenuid:241}]

Related Article

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.