forum

Home / DeveloperSection / Forums / Loop through json result and sort in numeric order in java script

Loop through json result and sort in numeric order in java script

Anonymous User202104-Apr-2013
Hi All!

This is my json:

{"event": { 
"items": [
            {"position": "2", "name": "John Doe 1"},
            {"position" : "1", "name": "John Doe 2"},
            {"position": "3", "name": "John Does 3"}
            ]
        }
This is how I loop through results after reading the json result:

$.each(data.event.items, function(val) {
 $('#list').append('<li>'+data.event.items[val].name+'</li>');
});
Right now the order is: John Doe 1, John Doe 2, John Doe 3. I want to loop them and display them in order of the position that is has been given. So the 

correct order would be John Doe 2, John Doe 1, John Doe 3.

How can I achieve that?
Thanks in advance!

Updated on 04-Apr-2013
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By