srikanth
Posts: 0
Joined: Tue Nov 20, 2012 6:18 am

How to sort/order a json response??

Building a store locator app with the google maps (static) distance matrix API to find the distance between the user location and list of predefined stores with lat long etc.

When we get the response we just map it to a list and all the rest works great.

We now need the closest store to be listed first. i.e in below case, the 1100km store will be listed as list item one and then the 2109km one...

is there any way i can do this coz google maps api doesnt have a sort option.

sample response:

{
"destination_addresses" : [ "lxyx" ],
"origin_addresses" : [ "lxyz", "Ixyz" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "2 109 km",
"value" : 2109034
},
"duration" : {
"text" : "1 jour 8 heures",
"value" : 114405
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "1 110 km",
"value" : 1109948
},
"duration" : {
"text" : "18 heures 33 minutes",
"value" : 66785
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to sort/order a json response??

If the service doesn't support it, you could sort it on the client (with JavaScript).

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to sort/order a json response??

Hi again:)

The easiest way is to use this plugin https://github.com/padolsey/jQuery-Pl...

1) Create JS with the code from here https://github.com/padolsey/jQuery-Pl...

2) map service results to list (in code below it's [name=mobilelistitem]) , map field distance.value to invisible Label (this field will be used for sorting. In code below Label name is name = v

3) on service Success add next JS:

codefunction compareFunc(a, b) {
var first, second;
first = $("[name=v]", a).text();
second = $("[name=v]", b).text();
return +first - (+second);
}
var $li = $("[name=mobilelistitem]:gt(0)"); //:gt(0) - we don't need first element. it's used for mapping
$li.sortElements(compareFunc, function(){ return this;});/code

taucher
Posts: 0
Joined: Fri Apr 17, 2015 6:06 am

How to sort/order a json response??

Hello, i have the same issue. I have a column in JSON format in my database containing score,picture,age... I couldn't sort it in server side.

In my app, I have a list with name, picture and score of the users getting from server I map the score field to counter value. How can i sort the list according to score.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How to sort/order a json response??

Hi,

Do you need sort data by content value? You can also do it via the database (https://devcenter.appery.io/documenta...)

taucher
Posts: 0
Joined: Fri Apr 17, 2015 6:06 am

How to sort/order a json response??

Yes by content value. But the data is in a column containing multiple values in json format in database. So i can not sort with sql type sorting.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How to sort/order a json response??

You should write your own sort function according to your requirements: http://stackoverflow.com/questions/97...

S Butler
Posts: 0
Joined: Mon Nov 17, 2014 4:44 pm

How to sort/order a json response??

Hi,

I added the code that Maryna provided above and works great. How can I adjust Maryna's code to accomplish random sorting? Thanks!

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to sort/order a json response??

The random funtion would go into the code from GitHub. We don't have the exact code to use.

Return to “Issues”