I am trying to sort a series of sentinel 2 images based on their average NDWI in the google earth engine. But nothing I do in sorting seems to work. Here I ll show what i tried:

's2_ndwi_mean' is a list of objects with 2 properties:

  • 'NDWI' which is the average mean NDWI
  • 'system:time_start' which is the time the image was taken.

This variable forms the starting point for this example.

Sorting a single dimensional array works:

for (var i = 0; i < 10; i++) {
    var ndwi_test = ee.Number(ee.Dictionary(s2_ndwi_mean.get(i)).get('NDWI')).float()

ndwi_array= ee.Array(ndwi_array).sort()

However sorting a list of objects based on their property does not seem to work. I tried a similar approach to this:

cars = [
        name: "Honda",
        speed: 80

        name: "BMW",
        speed: 180

cars.sort(function(a, b) { 
    return a.speed - b.speed;

But I cannot get this to work for earth engine list types.

Then i tried sorting a two dimensional array instead of a list. This is how it should work for a Javascript Array. But this seemed impossible for an earth engine array:

var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']];


function sortFunction(a, b) {
    if (a[0] === b[0]) {
        return 0;
    else {
        return (a[0] < b[0]) ? -1 : 1;

Does anybody know what the approach should be. Sorting a list of images based on computed average NDWI's?


If I understand correctly, 's2_ndwi_mean' is a list of objects ee.Image with a property called 'NDWI' which is the average mean NDWI.

var collection = ee.ImageCollection.fromImages(s2_ndwi_mean)
var sorted_asc = collection.sort('NDWI')
var sorted_desc = collection.sort('NDWI', false)
  • I know it's simple, but that's what I think the question is asking – Rodrigo E. Principe Jul 10 '18 at 12:46
  • Thanks, the question was indeed solved by making an Imagecollection from the list and having NDWI as a meta property. – Rogier Oct 15 '18 at 10:02

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.