這應該給你一個想法。但在調用json_encode之前,使用multisort在php中對數組進行排序會更有效。
var arr = [
{"id":"1498","tradingname":"Golden Shutter Photography1","listed":"2012-01-26 19:26:24","pictureurl":"","business_id":"1499","storeid":"1","phone":"6143737477","street":"122 Avebery Drive","suburb":"Berwick ","state":"1","postcode":"3806","discription":"","long":"","lat":"","offer":"200|60","tstamp":"2012-01-26 19:26:24","offers":"200|40"},
{"id":"1499","tradingname":"Golden Shutter Photography2","listed":"2012-01-26 19:26:24","pictureurl":"","business_id":"1499","storeid":"1","phone":"6143737477","street":"122 Avebery Drive","suburb":"Berwick ","state":"1","postcode":"3806","discription":"","long":"","lat":"","offer":"400|50","tstamp":"2012-01-26 19:26:24","offers":"200|50"},
{"id":"1500","tradingname":"Golden Shutter Photography3","listed":"2012-01-26 19:26:24","pictureurl":"","business_id":"1499","storeid":"1","phone":"6143737477","street":"122 Avebery Drive","suburb":"Berwick ","state":"1","postcode":"3806","discription":"","long":"","lat":"","offer":"200|50","tstamp":"2012-01-26 19:26:24","offers":"400|50"}
];
function sortbyoffer(a,b) {
var aa = a.offer.split("|");
var bb = b.offer.split("|");
if(aa[0] == bb[0]) {
return aa[1] - bb[1];
} else {
return aa[0] - bb[0];
}
}
arr.sort(sortbyoffer);
for(var n=0;n<arr.length;n++){
document.write(arr[n].id + ' ' + arr[n].tradingname + ' ' + arr[n].offer + '<br>');
}
輸出爲:
1500 Golden Shutter Photography3 200|50
1498 Golden Shutter Photography1 200|60
1499 Golden Shutter Photography2 400|50
@rdlowrey我已經試過在MySQL的指向訂單,並使用jquery的.sort – RussellHarrower 2012-02-14 03:52:09
爲什麼在JavaScript中的訂單排序?在php中使用multisort在使用json_encode之前對數組進行排序。 – busypeoples 2012-02-14 03:55:59