How to sort objects by key value

When developing a tree-like display model for ad hoc query, it is necessary to sort the manufacturers in the order of az. After going through Baidu, a total of a method is made for reference only.
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, which needs to be called with Object, and pass in the object that needs to get the key value. The keys method will return an array containing all the 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)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326588741&siteId=291194637