How can I sort JSON data?

edited August 2014 in How To...

Hi,

I've been developing a Processing project which visualises earthquake data by plotting epicentres on a map. The earthquakes are shaded according to depth. I've got the basic program working and drawing stuff (yay!). My current program works its way through the JSON data which is organised by time, when it should draw earthquakes from deepest to shallowest - with shallow epicentres blocking deeper ones.

So I need to sort the JSON data by the depth field and then draw the results. Here's the top of the JSON feed showing the data for a single earthquake, including the vital depth field.

{
   "results":[
      {"timestamp":"2014-0829T23:24:20.000Z",
      "latitude":64.817,
      "longitude":-16.898,
      "depth":11.1,"
      size":0.8,
...
...

So is there any way of sorting the JSON data by a field, or can I convert it to another type and sort it? Or is there another, clever way to do it?

Many thanks for any help you can offer.

Mike.

Answers

  • Processing's Json support doesn't include sorting data, apparently.
    So, indeed, you will need to put your data in a instances of a class implementing Comparable, and sort the array list or array you create this way.

  • Okay thanks - now off to learn about Comparable - looks scary!

    Thanks again,

    Mike.

  • It is quite simple actually, and there are lot of forum messages about this, search from the main site (not from this forum).

Sign In or Register to comment.