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}]