var keysArr = Object.keys(listData.fTypeListData).sort(); var sortObj = {}; for(var i=0;i<keysArr.length;i++){ var key = keysArr [i]; sortObj[key] = listData.fTypeListData[key]; }
One thing to note is the keys() method. keys() is a method of Object. It needs to be called with Object, and the object that needs to get the key value is passed in. The keys method will return an array containing all keys and sort the array.
The sort() method is an array method. If no parameter is passed, it is sorted according to the sorting algorithm of the engine (if the first string is the same, the second one is compared, and so on. This is very practical!), you can pass in a method:
sort(function(a,b){ return ab; //ab, ascending order. ba, descending order. })
(ps:a represents the previous number in the array, b represents the next number in the array, if a>b, ab is greater than 0, a is placed in front of b, so ascending order)