how to sort a featureset?

2482
5
Jump to solution
10-10-2012 07:07 AM
SteveCole
Frequent Contributor
Has anyone found or written a function to take a featureSet and sort it based on a specified attribute? The only way I can think of right now would be to:


  1. create a dojo dGrid via code

  2. provide the featureSet as it's store

  3. sort the dGrid based on the appropriate column

  4. create a new featureSet based on the sorted store

Thanks-
Steve
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor
You really want to sort on the featureSet.features array.
You can pass a function to the sort method to determine your sort.
I use this, but there's probably a better way to do this, since I need a text sort, not numbers or dates.
var _sort = function(field) {       return function(a, b) {         var x, y;         x = a.attributes[field].toLowerCase();         y = b.attributes[field].toLowerCase();          return (x < y) ? -1 : 1;       }; };  _featureSet.features.sort(_sort('FIELD_NAME'));


edit, had wrong file in clipboard

View solution in original post

0 Kudos
5 Replies
__Rich_
Occasional Contributor III
Vanilla JS - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

Or if you want to use Dojo, then you could push the items into a store (no need for a UI component e.g. dGrid) and then fetch them back sorted - http://dojotoolkit.org/reference-guide/1.7/quickstart/data/usingdatastores/sorting.html#quickstart-d...

Either way, you're going to need to write some code....
0 Kudos
ReneRubalcava
Frequent Contributor
You really want to sort on the featureSet.features array.
You can pass a function to the sort method to determine your sort.
I use this, but there's probably a better way to do this, since I need a text sort, not numbers or dates.
var _sort = function(field) {       return function(a, b) {         var x, y;         x = a.attributes[field].toLowerCase();         y = b.attributes[field].toLowerCase();          return (x < y) ? -1 : 1;       }; };  _featureSet.features.sort(_sort('FIELD_NAME'));


edit, had wrong file in clipboard
0 Kudos
SteveCole
Frequent Contributor
Awesome! Thanks. I realized after posting that the datastore was more what I had in mind instead of the datagrid. At first, I thought that Rene's example would almost work for me since I do need to sort based on string values. My only catch is that I needed to sort by two fields (census tract being one and block group the other). As it turns out, there is a another field in my data which is a combination of the two which I had overlooked so Rene's code snippit works! Huzzah!

Thanks again!
Steve
0 Kudos
__Rich_
Occasional Contributor III
I needed to sort by two fields

Just for information, you could do that in the sort function. (I know you've got the concatenated field)
0 Kudos
deleted-user-pYdfRjvSAbTJ
New Contributor III
featureset is basically an array, you can use LinqJS to do everything you want with it
http://linqjs.codeplex.com/
0 Kudos