0

I am working on this code and need to sort the JSON output by a value, in this case called cars. How can I accomplish it in this code?

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);
2
  • 1
    Please show a sample of your json Commented Jun 4, 2019 at 3:42
  • What do you mean "sort" - sort what data and by which criteria? Commented Jun 4, 2019 at 3:59

1 Answer 1

1

I think this code will give you the result you were looking for:

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Virgilio, thanks for the answer but that did not work for some reason and I also did not see any errors in the console.
Try to debug and check the array before and after sort. Is it not sorting by the property _serverMessages[0].car as you wanted?
Virgilio, the content is not shown at all. I will try to debug and will let you know what I learn. Thanks again!
Hey Virgilio, sorry for the late response but I can't still get it to work. I got an example from my json response, I wanted to sort by the originalarrivaltime in this example: pastebin.com/1zysxJ0C. Any other idea? Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.