Javascript 循环中的JSON解析排序

Javascript 循环中的JSON解析排序,javascript,json,Javascript,Json,我正在编写这段代码,需要按一个值对JSON输出进行排序,在本例中称为cars。我如何在这段代码中实现它 var txtFile = new XMLHttpRequest(); txtFile.open("GET", "test.txt", true); txtFile.onreadystatechange = function() { if (txtFile.readyState === 4) { if (txtFile.status === 20

我正在编写这段代码,需要按一个值对JSON输出进行排序,在本例中称为cars。我如何在这段代码中实现它

var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "test.txt", true);
    txtFile.onreadystatechange = function() {
      if (txtFile.readyState === 4) {  
        if (txtFile.status === 200) {  // Makes sure it's found the file.
          allText = txtFile.responseText;
          lines = txtFile.responseText.split("\n"); // Will separate each line into an array

          for (var i = 0; i <= lines.length; i++){

              var obj = JSON.parse(lines[i]);

                document.getElementById("demo").innerHTML += obj._serverMessages[0].car + "         " + obj.creator.substr(2, obj.creator.length) + " / " + obj._serverMessages[0].content + "<br>";


          }
        }
      }
    }
    txtFile.send(null);
var txtFile=new XMLHttpRequest();
打开(“GET”,“test.txt”,true);
txtFile.onreadystatechange=函数(){
如果(txtFile.readyState==4){
if(txtFile.status==200){//确保找到该文件。
allText=txtFile.responseText;
lines=txtFile.responseText.split(“\n”);//将每一行分隔成一个数组

对于(var i=0;i我认为这段代码将为您提供您想要的结果:

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "test.txt", true);
txtFile.onreadystatechange = function() {
    if (txtFile.readyState === 4) {  
        if (txtFile.status === 200) {  // Makes sure it's found the file.
            allText = txtFile.responseText;
            lines = txtFile.responseText.split("\n"); // Will separate each line into an array

            var array = [];
            for (var i = 0; i <= lines.length; i++) {
                array.push(JSON.parse(lines[i]))
            }
            array.sort(function(a, b){
                if (a._serverMessages && a._serverMessages.length && a._serverMessages[0] && a._serverMessages[0].car) {
                    if (b._serverMessages && b._serverMessages.length && b._serverMessages[0] && b._serverMessages[0].car) {
                        return a._serverMessages[0].car > b._serverMessages[0].car? 1 : -1;
                    } else return -1;
                } else return 1;
            });

            for (var i = 0; i <= array.length; i++) {
                document.getElementById("demo").innerHTML += array[i]._serverMessages[0].car + "         " + array[i].creator.substr(2, array[i].creator.length) + " / " + array[i]._serverMessages[0].content + "<br>";
            }
        }
    }
}
txtFile.send(null);
var txtFile=new XMLHttpRequest();
打开(“GET”,“test.txt”,true);
txtFile.onreadystatechange=函数(){
如果(txtFile.readyState==4){
if(txtFile.status==200){//确保找到该文件。
allText=txtFile.responseText;
lines=txtFile.responseText.split(“\n”);//将每一行分隔成一个数组
var数组=[];
对于(VARI=0;IB._服务器消息[0]。car?1:-1;
}否则返回-1;
}否则返回1;
});

对于(var i=0;我请展示一个JSON的示例“排序”是什么意思?排序什么数据以及根据哪些标准?Virgilio,谢谢你的回答,但由于某种原因,这不起作用,我也没有在控制台中看到任何错误。尝试在排序之前和之后调试和检查数组。它不是按属性_serverMessages[0]排序吗.car如您所愿?Virgilio,内容根本没有显示。我将尝试调试并让您知道我学到了什么。再次感谢!嘿,Virgilio,很抱歉响应太晚,但我仍然无法使其正常工作。我从json响应中得到了一个示例,我想在此示例中按原始到达时间排序:。还有其他想法吗?谢谢!