2013-07-10 196 views
0

我有一個JSON object.the對象像下面如何對json對象進行排序?

{"route":[ 
{"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"}, 
{"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"}, 
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"}, 
{"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"}} 
{"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}} 

我想排序相對於按升序排列行和coloums這個對象給出。就像我想得到的對象是像下面給出的

{"route":[ 
{"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"41","row":"0","width":"1","zIndex":"0"}, 
{"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"37","row":"1","width":"1","zIndex":"0"}, 
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"}, 
{"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"29","row":"3","width":"1","zIndex":"0"}} 
{"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"29","row":"4","width":"1","zIndex":"0"}} 

有人可以幫我出來嗎?

+1

你已經採取了看看這個http://stackoverflow.com/questions/4222690/sorting-a -json-object-in-javascript? –

+6

沒有像「json對象」那樣的東西。有json字符串。可以將其解碼爲本地對象。 –

+1

看看['[] .sort'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)。傳遞一個函數來比較'obj.route'數組中每個元素的'列'值。有一個在文檔中排序對象數組的例子。 –

回答

1

鑑於所提供的對象:

var obj = { 
    "route" : [ 
     {"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"}, 
     {"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"}, 
     {"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"}, 
     {"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"}, 
     {"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"} 
    ] 
}; 

您需要實現陣列上的一種功能類似如下:

如果列接管排優先級,那麼你首先檢查列的值。如果相等,則檢查該行的值。

obj.route.sort(function(a, b) { 
    if (a.column == b.column) { 
     return a.row == b.row ? 0 : +a.row > +b.row ? 1 : -1; 
    } 

    return +a.column > +b.column ? 1 : -1; 
}); 

如果行優先於列:

obj.route.sort(function(a, b) { 
    if (a.row == b.row) { 
     return a.column == b.column ? 0 : +a.column > +b.column ? 1 : -1; 
    } 

    return +a.row > +b.row ? 1 : -1; 
}); 

FIDDLE DEMO

+0

謝謝你..它完美的工作! – user2499644

+0

@crush在參考文獻之前加上那些加號「+」的目的是什麼? (例如'+ a.row','+ b.row')提前致謝。 – KiriSakow

+1

@KiriSakow它們是一元運算符,它將值轉換爲數字(如果它最初是字符串的話)。 – crush

2
var o = {"route":[ 
{"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"}, 
{"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"}, 
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"}, 
{"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"}, 
{"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}]}; 

var r = o.route; 
// sort 
var sorted = r.sort(function (a, b) { 
    a = parseInt(a.column, 10); 
    b = parseInt(b.column, 10); 
    return a < b ? -1 : a > b ? 1 : 0; 
}); 
console.log(sorted); 
+0

感謝這對我完美:) –

相關問題