2012-02-14 59 views
0

所以我們有一個JSON字符串,看起來像下面。javascript訂單JSON由SUM

[{"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 | 9100 | 10

在這種情況下,我們希望移動100 | 9月底太像這樣

100 | 20,100 | 10,100 | 9

任何幫助將是巨大的

+0

@rdlowrey我已經試過在MySQL的指向訂單,並使用jquery的.sort – RussellHarrower 2012-02-14 03:52:09

+0

爲什麼在JavaScript中的訂單排序?在php中使用multisort在使用json_encode之前對數組進行排序。 – busypeoples 2012-02-14 03:55:59

回答

0

這應該給你一個想法。但在調用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