我想通过data.model按字母顺序对JSON表进行排序,而不是通过fuelState实际排序。我测试了很多东西,但实际上没有什么工作正确......有一个简单的解决方案吗?
//..//
<body>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function callJSON() {
setTimeout(function () {
$.getJSON('./url.php', function(data) {
$ ("#usertable").empty();
var cars = data.rec.vehicles.vehicles;
cars.sort(function(a,b) {
return parseFloat(b.fuelState) - parseFloat(a.fuelState);
});
$.each(cars, function(key, data){
//..//
$('#usertable').append(
function() {
return "<tr class="+farbe+">"+
"<td>"+data.model+coopers+"</td>"+
//..//
"<tr>";
}
);
})
}).always(callJSON);
}, 2000);
}
$(callJSON);
});
</script>
</body>
</html>
测试的东西:
function compare(el1, el2, index) {
return el1[index] == el2[index] ? 0 : (el1[index] < el2[index] ? -1 : 1);
}
var cars = data.rec.vehicles.vehicles;
cars.sort(function(el1,el2){
return compare(el1, el2, "data.model")
});