0

I have some ids as list and I want to filter them in LUCAS dataset. I made a simple example for my problem.

After filtering, I want results as original-initial order of searchList. But after filtering, order of features are not in initial order of searchList. It is important for me to keep initial orders of features.

How can I solve this?

var geometry = ee.Geometry.Polygon(
    [[[26.086635245164302, 41.088934497428085],
      [26.086635245164302, 41.03457263065797],
      [26.14911998637524, 41.03457263065797],
      [26.14911998637524, 41.088934497428085]]], null, false);

var dataset = ee.FeatureCollection("JRC/LUCAS_HARMO/THLOC/V1")
          .filterBounds(geometry)
          //.select('id')

print(dataset)
Map.addLayer(dataset, {}, "LUCAS Points (data)", true);

var searchList = ee.List([950258, 274091, 418881])

var results1 = dataset.filter(ee.Filter.inList('id', searchList))

print(results1)
// results are in order 418881, 950258, 274091

//But results must be in order, 950258, 274091, 418881

Code

2
  • What do you mean by initial order? And why is it important? You can sort into ascending id order with this: results1.sort("id"). But I don't know if this is what you are looking for.
    – JonasV
    Commented Sep 20, 2021 at 8:13
  • My initial order means, order of search list. If result1 contain searchList elements, results must be in initial-original order of features Commented Sep 20, 2021 at 8:19

1 Answer 1

1

One idea is to map a function onto your filtered dataset that sets as a new property the position of id in sortList, which can be then used to sort dataset:

var results1 = dataset.filter(ee.Filter.inList('id', searchList))
  .map(function(feature){
    return feature.set('sortID',searchList.indexOf(feature.get('id')))
    })
  .sort('sortID')
4
  • Thank you very much, it is working Commented Sep 21, 2021 at 7:39
  • There is an explanation, stackoverflow.com/questions/39712160/…, javascript preserves initial order of elements after filtering. Why don't be this in GEE? Commented Sep 21, 2021 at 8:04
  • 1
    The results are (always) in the same order as they were in the original collection before the filtering. Your list of IDs is selecting the 1st, 9th and 14th feature from the original collection, and they're coming out in that order. If you want them in some other order, then, as suggested, you need to sort them into the order you want. Commented Sep 21, 2021 at 12:34
  • Thank you very much, Noel Commented Sep 21, 2021 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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