# Sorting based on computed property (NDWI)

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.push(ndwi_test)
}

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']];

a.sort(sortFunction);

function sortFunction(a, b) {
if (a === b) {
return 0;
}
else {
return (a < b) ? -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)