2

I am trying to sort the objects in a geoJSON FeatureCollection alphabetically by the values of one of their properties. How do I get to the nested object properties with JavaScript? I have tried using .sort to no avail.

Sample geoJSON to sort on PARK_NAME property:

"type" : "FeatureCollection",

"features" : [{
    "type" : "Feature",
    "id" : 1,
    "properties" : {
        "CRAG_NAME" : "Alberton Road Rock",
        "PARK_NAME" : "Patapsco State Park",
        "LAT_DD" : 39.3168,
        "LONG_DD" : -76.8033,
        "GUIDE_URL" : "http://indy-adventures.net/climbing/climb-maryland/guide.html#Alberton",
        "OTHER_URL" : "http://www.dnr.state.md.us/publiclands/central/patapsco.asp"
    },
    "geometry" : {
        "type" : "Point",
        "coordinates" : [-76.8033, 39.3168]
    }
}, {
    "type" : "Feature",
    "id" : 2,
    "properties" : {
        "CRAG_NAME" : "Annapolis Rocks",
        "PARK_NAME" : "Appalachian Trail",
        "LAT_DD" : 39.5583,
        "LONG_DD" : -77.5987,
        "GUIDE_URL" : "http://indy-adventures.net/climbing/guide.html#AnnapolisRocks",
        "OTHER_URL" : "http://www.dnr.state.md.us/publiclands/at.asp"
    },
    "geometry" : {
        "type" : "Point",
        "coordinates" : [-77.5987, 39.5583]
    }
}]  
}

.....

1
3

In case someone might be looking for a generic way to sort GeoJSON by one or more properties, you might find this function useful (NB is case sensitive in current form):

<array>.sort(propSort("PARK_NAME"));

// Would sort by PARK_NAME

<array>.sort(propSort(["PARK_NAME", "CRAG_NAME"]));

// Would sort by PARK_NAME and then by CRAG_NAME

function propSort(props) {
  if (!props instanceof Array) props = props.split(",");
  return function sort(a, b) {
    var p;
    a = a.properties;
    b = b.properties;
    for (var i = 0; i < props.length; i++) {
      p = props[i];
      if (typeof a[p] === "undefined") return -1;
      if (a[p] < b[p]) return -1;
      if (a[p] > b[p]) return 1;
    }
    return 0;
  };
}
2

Sort is executed as:

<array>.sort(sortfunction)

The sortfunction is optional, and tells the sort how to compare two objects. If the first object should come before the second object in an ascending sort, then the sortfunction should return a value less than 0 (convention is -1) for the comparison. If the first object should come after the second object in an ascending sort, then the sortfunction should return a value greater than 0 (convention is 1) for the comparison. If the two objects are equivalent and either one can come before the other in an ascending sort, then the sortfunction should return a value of 0.

In this case, you want to access the underlying properties of the object, so you would write your sort function like this:

function sortByPARK_NAME(a,b) {
    return (a.PARK_NAME.toUpperCase() < b.PARK_NAME.toUpperCase()) ? -1 : ((a.PARK_NAME.toUpperCase() > b.PARK_NAME.toUpperCase()) ? 1 : 0);

The inclusion of toUpperCase makes the sort case insensitive. With this sortfunction, your sort would be executed as

features.sort(sortByPark_Name);

In the linked solution in the comments, there is a more generic way of doing this: https://stackoverflow.com/a/979325/2033188

To use that solution for your specific problem, you would do:

features.sort(sort_by('PARK_NAME',false,function(a){return a.toUpperCase()}));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.