4

I have a list with two-segment.

var list = ee.List([
  [101, 102],
  [102, 103],
  [103, 104],
  [106, 100],
  [104, 105],
  [105, 106],
  ])

How can sort this list by first or second column?

1 Answer 1

5

You strip off of the column you want to sort by, then use it as the "keys" argument to list.sort.

var sortColumn = 0;
var keyValues = list.map(function(inner) {
    return ee.List(inner).get(sortColumn)
})
var sorted = list.sort(keyValues)
3
  • i am grateful, thank you very much. I think, you can implement this function to work with arrays and lists in GEE. Thank you very much, dear Noel Apr 3, 2021 at 17:31
  • Dear Noel, your code is worked, but if the list has same two elements, this code does not display it, it display distinct of list, not all of the list, for example; if list is : <br/> var list = ee.List([ [101, 102], [102, 103], [103, 104], [106, 100], [104, 105], [105, 106], [101, 102], [102, 103], [103, 104] ]) it display 6 elements again Apr 4, 2021 at 11:05
  • You are correct; that's a bug in the list.sort method. I've filed a bug report and started on a fix. Apr 5, 2021 at 13:12

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.