2016-08-31 11 views
1

私は次のようなJSONjsonデータを特定の値に基づいてソートする方法は?

function nextDepartures(minutes=120) 
{ 
var modeId = 0; 
var stopId = 1042; 
var limit = 2; 

xhr(broadNextDeparturesURL(modeId, stopId, limit), broadNextDeparturesCallback); 

var nowDate = new Date(); 
nowDate.setMinutes(nowDate.getMinutes()+minutes); 



for(var i =0; i < broadjson[0].values.length;i++) { 
    var date = new Date(broadjson[0].values[i].time_timetable_utc); 
    var minutes; 
    if (date < nowDate) { 
     if (date.getMinutes() < 10) { 
      minutes = "0" + date.getMinutes(); 
     } 
     else { 
      minutes = date.getMinutes(); 
     } 

    else { 

     document.getElementById("depart").innerHTML += "<tr>" + 
      "<td width='30%'>" + date.getHours() + ":" + minutes + "</td>" + 
      "<td>" + broadjson[0].values[i].platform.direction.line.line_number + 
      " " 
      + broadjson[0].values[i].platform.direction.direction_name + "</td>" + 

      "</tr>"; 
    } 
    } 
} 
} 

を取得するには、このコードを使用していますし、見返りに、私はこのデータを取得しています

{ 
 
"values": [ 
 
{ 
 
"platform": { 
 
"realtime_id": 0, 
 
"stop": { 
 
"distance": 0.0, 
 
"suburb": "East Melbourne", 
 
"transport_type": "train", 
 
"route_type": 0, 
 
"stop_id": 1104, 
 
"location_name": "Jolimont-MCG", 
 
"lat": -37.81653, 
 
"lon": 144.9841 
 
}, 
 
"direction": { 
 
"linedir_id": 38, 
 
"direction_id": 5, 
 
"direction_name": "South Morang", 
 
"line": { 
 
"transport_type": "train", 
 
"route_type": 0, 
 
"line_id": 5, 
 
"line_name": "South Morang", 
 
"line_number": "250", 
 
"line_name_short": "South Morang", 
 
"line_number_long": "" 
 
} 
 
} 
 
}, 
 
"run": { 
 
"transport_type": "train", 
 
"route_type": 0, 
 
"run_id": 15716, 
 
"num_skipped": 0, 
 
"destination_id": 1041, 
 
"destination_name": "Clifton Hill" 
 
}, 
 
"time_timetable_utc": "2016-03-16T01:51:00Z", 
 
"time_realtime_utc": null, 
 
"flags": "", 
 
"disruptions": "" 
 
} 
 
] 
 
}

私は

に応じてこのデータをソートしたいです

values.platform.direction.line.line_number

これは、最低の行番号が最初に表示され、最後に最高の行番号が表示されることを意味します。私はjavascript関数のソート(a、b)を試みましたが、動作しません。問題は

values.platform.direction.line.line_number

が文字列値であるということです。どのように配列を並べ替えることができますline_number整数を返しますが、文字列の形ですか?

+0

使用あなたは値をのparseIntと比較することができ、比較のparam –

+0

チェックこの回答http://stackoverflow.com/questions/881510/sorting-json-by-values – frankfg

+0

とJavaScriptのソート機能JavaScriptのソート機能を使用しています。 – z0mBi3

答えて

2

これまで同様の質問がstackoverflowで尋ねられました。このリンクをチェックしてください。 How to sort JSON by a single integer field?

+0

問題はオブジェクトが文字列値で、整数値としてソートしたいのです – Gurminder

+0

JavaScriptを使用してソートを試みることができます。ここでは、文字列の値をこのNumber(value)のようなnumberに直接型キャストすることができます。 – Phoenix

2

parseIntを使用して渡された文字列を解析し、javascript sort関数を使用して以下のように配列をソートします。

myArray.sort(function(a, b){ 
    var lineA = parseInt(a.line_number), 
     lineB = parseInt(b.line_number); 
    if(lineA < lineB) return -1; 
    if(lineA > lineB) return 1; 
    return 0; 
}); 
1

デルタがline_numberで直接ソートできます。マイナスは両方の部分を番号にキャストします。

var data = { 
 
     values: [ 
 
      { platform: { direction: { line: { line_number: "250" } } } }, 
 
      { platform: { direction: { line: { line_number: "150" } } } }, 
 
      { platform: { direction: { line: { line_number: "110" } } } } 
 
     ] 
 
    }; 
 

 
data.values.sort(function (a, b) { 
 
    return a.platform.direction.line.line_number - b.platform.direction.line.line_number; 
 
}); 
 

 
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

関連する問題