---
title: "Loop through json result and sort in numeric order in java script"  
description: "Loop through json result and sort in numeric order in java script"  
author: "Anonymous User"  
published: 2013-04-04  
updated: 2013-04-04  
canonical: https://www.mindstick.com/forum/708/loop-through-json-result-and-sort-in-numeric-order-in-java-script  
category: "javascript"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# Loop through json result and sort in numeric order in java script

Hi All!\
This is my json:\
{"[event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london)": { "[items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios)": [ {"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](https://yourviews.mindstick.com/story/4497/usage-of-baking-soda-magical-results) after [reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) the json [result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency):\
$.each(data.event.items, [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server)(val) { $('#list').append('<li>'+data.event.items[val].name+'</li>');});[Right now](https://answers.mindstick.com/qa/36863/what-would-happen-if-gravity-became-5-percent-stronger-right-now) the [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) 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](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by Vijay Shukla

Hi Ben!\
You will need to sort the items first:\
data.event.items.sort(function(item_a, item_b) { return item_a.position - item_b.position;});

### Reply by AVADHESH PATEL

Hi Ben!\
you can try this way\
sort your items before building the display \
data.event.items.sort(function(a,b) { return a.position-b.position; });Demonstration (click "Run with JS")\
Please note that there is no JSON in your problem, data is a plain JavaScript object.\
I hope it resolve your problem


---

Original Source: https://www.mindstick.com/forum/708/loop-through-json-result-and-sort-in-numeric-order-in-java-script

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
