If the objects come from the background, the order is disrupted
In other words, the object has multiple properties. In this page, you need to sort by id, and in another page, you need to sort by quantity
Here we use the Unicode attribute of characters to sort
sort provides related methods
<table> <thead> <tr> <th>id</th> <th>name</th> <th>num</th> <th>level</th> </tr> </thead> <tbody> </tbody> </table>
//Data to be sorted var data = [{ "id": 1, "name": "abc", "num": 123, "level": "1234" //Unicode value \ u4e00\u4e8c\u4e09\u56db }, { "id": 5, "name": "acb", "num": 456, "level": "One turn" //Unicode value }, { "id": 2, "name": "bac", "num": 234, "level": "Success work" //Unicode value \ u6210\u529f\u529f }, { "id": 3, "name": "a12", "num": 345, "level": "Seven days" //Unicode value \ u4e03\u65e5 }, { "id": 4, "name": "a32", "num": 567, "level": "week" //Unicode value \ u793c\u62dc }] //Call the sorting method to sort by level //true for ascending sort, and false for descending sort //When the position of the second parameter does not pass a value, it is sorted in ascending order by default data.sort(sortBy('level', true)); //Render data re(data); function re(data) { var str = ""; for (var i = 0; i < data.length; i++) { str += '<tr>' + ' <td>' + data[i].id + '</td>' + ' <td>' + data[i].name + '</td>' + ' <td>' + data[i].num + '</td>' + ' <td>' + data[i].level + '</td>' + '</tr>' } $("tbody").html(str) } function sortBy(attr, rev) { //The second parameter does not pass the default ascending arrangement if (rev == undefined) { rev = 1; } else { rev = (rev) ? 1 : -1; } return function (a, b) { a = a[attr]; b = b[attr]; if (a < b) { return rev * -1; } if (a > b) { return rev * 1; } return 0; } }
- Results sorted by level
- Chinese characters are sorted according to the Unicode code of Chinese characters
- At present, there is no way to sort Chinese characters according to Pinyin. I hope you can give me some advice
- Unicode URL attached http://tool.chinaz.com/tools/...
- Results sorted by id
- Results sorted by name
- Results sorted by num