Page 1 of 1

How to sort/order a json response??

Posted: Fri Mar 22, 2013 6:51 pm
by srikanth

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"
}


How to sort/order a json response??

Posted: Fri Mar 22, 2013 9:00 pm
by maxkatz

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


How to sort/order a json response??

Posted: Fri Mar 22, 2013 9:08 pm
by Maryna Brodina

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


How to sort/order a json response??

Posted: Mon Jun 22, 2015 9:24 am
by taucher

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.


How to sort/order a json response??

Posted: Tue Jun 23, 2015 10:40 am
by Serhii Kulibaba

Hi,

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


How to sort/order a json response??

Posted: Tue Jun 23, 2015 10:56 am
by taucher

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.


How to sort/order a json response??

Posted: Fri Jun 26, 2015 9:24 am
by Serhii Kulibaba

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


How to sort/order a json response??

Posted: Fri Feb 05, 2016 2:07 am
by S Butler

Hi,

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


How to sort/order a json response??

Posted: Fri Feb 05, 2016 4:31 am
by maxkatz

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