所以我们得到的是一个JSON字符串,如下所示:
[{"id":"1499","tradingname":"Golden Shutter Photography","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":"500|50","tstamp":"2012-01-26 19:26:24","offers":"500|50"}]我们需要的是一种JavaScript方式来对结果进行排序/混洗,这样他们就可以接受报价并将其排序,从而在最后显示最大的差异,在第一个显示最小的差异。
例如,假设JSON中的提议如下所示
100|10
100|20
100|9它会按如下顺序排列
100|20
100|10
100|9现在的问题是,有些报价超过1个报价,因此它们如下所示
100|20,100|9,100|10在本例中,我们希望将100|9移到末尾,如下所示
100|20,100|10,100|9发布于 2012-02-14 12:55:38
这应该会给你一个想法。但是在调用json_encode之前,在php中使用multisort对数组进行排序会更有效。
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|50https://stackoverflow.com/questions/9271383
复制相似问题