Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
Stay organized with collections
Save and categorize content based on your preferences.
Sorts elements of each array pixel along one axis.
Usage
Returns
Image.arraySort(keys)
Image
Argument
Type
Details
this: image
Image
Array image to sort.
keys
Image, default: null
Optional keys to sort by. If not provided, the values are used as the keys. The keys can only have multiple elements along one axis, which determines the direction to sort in.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[[["\u003cp\u003e\u003ccode\u003eImage.arraySort()\u003c/code\u003e sorts elements within each pixel's array along a single axis, either ascending or descending.\u003c/p\u003e\n"],["\u003cp\u003eBy default, it sorts array elements in ascending order based on their values; an optional \u003ccode\u003ekeys\u003c/code\u003e argument allows for custom sorting based on another array image.\u003c/p\u003e\n"],["\u003cp\u003eWhen sorting 2D arrays, the \u003ccode\u003ekeys\u003c/code\u003e array image defines the sorting direction and order, allowing you to sort by rows or columns.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ekeys\u003c/code\u003e array's dimensions determine the sorting axis: a 1xN array sorts along rows, and an Nx1 array sorts along columns of the input array image.\u003c/p\u003e\n"],["\u003cp\u003eThe values in the \u003ccode\u003ekeys\u003c/code\u003e array determine the relative positions of elements in the sorted output array.\u003c/p\u003e\n"]]],[],null,["# ee.Image.arraySort\n\nSorts elements of each array pixel along one axis.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-----------------------------|---------|\n| Image.arraySort`(`*keys*`)` | Image |\n\n| Argument | Type | Details |\n|---------------|----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | Array image to sort. |\n| `keys` | Image, default: null | Optional keys to sort by. If not provided, the values are used as the keys. The keys can only have multiple elements along one axis, which determines the direction to sort in. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A function to print arrays for a selected pixel in the following examples.\nfunction sampArrImg(arrImg) {\n var point = ee.Geometry.Point([-121, 42]);\n return arrImg.sample(point, 500).first().get('array');\n}\n\n// Create a 1D array image with length 12.\nvar arrayImg1D = ee.Image([0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]\n\n// Sort the 1D array in ascending order.\nvar arrayImg1DSorted = arrayImg1D.arraySort();\nprint('1D array image sorted (pixel)', sampArrImg(arrayImg1DSorted));\n// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n\n// You can use another array image to control sorting. The order of values\n// in the optional keys array translates to relative position in the input\n// array. For example, here is a keys array image with non-sequential values in\n// descending order, it will reverse the order of the input array; make the\n// last position first and the first position last.\nvar keys1D = ee.Image([99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]).toArray();\nprint('1D array image keys (pixel)', sampArrImg(keys1D));\n// [99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]\nvar arrayImg1DSortedKeys = arrayImg1D.arraySort(keys1D);\nprint('1D array image sorted by keys (pixel)', sampArrImg(arrayImg1DSortedKeys));\n// [3, 8, 9, 2, 1, 11, 7, 4, 5, 6, 10, 0]\n\n// To sort a 2D array, the keys array image is required.\n// Create a 2D array image with 3 rows and 4 columns.\nvar arrayImg2D = arrayImg1D.arrayReshape(ee.Image([3, 4]).toArray(), 2);\nprint('2D array image (pixel)', sampArrImg(arrayImg2D));\n// [[0, 10, 6, 5],\n// [4, 7, 11, 1],\n// [2, 9, 8, 3]]\n\n// Only a single axis can be sorted at a time. Here, we reverse sort along rows.\n// There are 4 elements in each row, so we construct an 1x4 array image.\nvar keys2Drows = ee.Image([3, 2, 1, 0]).toArray().toArray(1).arrayTranspose();\nprint('2D array image row keys (pixel)', sampArrImg(keys2Drows));\n// [[3, 2, 1, 0]]\nvar arrayImg2DSortedRows = arrayImg2D.arraySort(keys2Drows);\nprint('2D array image sorted rows (pixel)', sampArrImg(arrayImg2DSortedRows));\n// [[5, 6, 10, 0],\n// [1, 11, 7, 4],\n// [3, 8, 9, 2]]\n\n// Reverse sort along columns, create a 3x1 keys array image with values in\n// descending order.\nvar keys2Dcols = ee.Image([2, 1, 0]).toArray().toArray(1);\nprint('2D array image column keys (pixel)', sampArrImg(keys2Dcols));\n// [[2],\n// [1],\n// [0]]\nvar arrayImg2DSortedCols = arrayImg2D.arraySort(keys2Dcols);\nprint('2D array image sorted cols (pixel)', sampArrImg(arrayImg2DSortedCols));\n// [[2, 9, 8, 3],\n// [4, 7, 11, 1],\n// [0, 10, 6, 5]]\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# A function to print arrays for a selected pixel in the following examples.\ndef samp_arr_img(arr_img):\n point = ee.Geometry.Point([-121, 42])\n return arr_img.sample(point, 500).first().get('array')\n\n# Create a 1D array image with length 12.\narray_img_1d = ee.Image([0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]\n\n# Sort the 1D array in ascending order.\narray_img_1d_sorted = array_img_1d.arraySort()\nprint('1D array image sorted (pixel):',\n samp_arr_img(array_img_1d_sorted).getInfo())\n# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n\n# You can use another array image to control sorting. The order of values\n# in the optional keys array translates to relative position in the input\n# array. For example, here is a keys array image with non-sequential values in\n# descending order, it will reverse the order of the input array; make the\n# last position first and the first position last.\nkeys_1d = ee.Image([99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]).toArray()\nprint('1D array image keys (pixel):', samp_arr_img(keys_1d).getInfo())\n# [99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]\narray_img_1d_sorted_keys = array_img_1d.arraySort(keys_1d)\nprint('1D array image sorted by keys (pixel):',\n samp_arr_img(array_img_1d_sorted_keys).getInfo())\n# [3, 8, 9, 2, 1, 11, 7, 4, 5, 6, 10, 0]\n\n# To sort a 2D array, the keys array image is required.\n# Create a 2D array image with 3 rows and 4 columns.\narray_img_2d = array_img_1d.arrayReshape(ee.Image([3, 4]).toArray(), 2)\nprint('2D array image (pixel):', samp_arr_img(array_img_2d).getInfo())\n# [[0, 10, 6, 5],\n# [4, 7, 11, 1],\n# [2, 9, 8, 3]]\n\n# Only a single axis can be sorted at a time. Here, we reverse sort along rows.\n# There are 4 elements in each row, so we construct an 1x4 array image.\nkeys_2d_rows = ee.Image([3, 2, 1, 0]).toArray().toArray(1).arrayTranspose()\nprint('2D array image row keys (pixel):', samp_arr_img(keys_2d_rows).getInfo())\n# [[3, 2, 1, 0]]\narray_img_2d_sorted_rows = array_img_2d.arraySort(keys_2d_rows)\nprint('2D array image sorted rows (pixel):',\n samp_arr_img(array_img_2d_sorted_rows).getInfo())\n# [[5, 6, 10, 0],\n# [1, 11, 7, 4],\n# [3, 8, 9, 2]]\n\n# Reverse sort along columns, create a 3x1 keys array image with values in\n# descending order.\nkeys_2d_cols = ee.Image([2, 1, 0]).toArray().toArray(1)\nprint('2D array image column keys (pixel):',\n samp_arr_img(keys_2d_cols).getInfo())\n# [[2],\n# [1],\n# [0]]\narray_img_2d_sorted_cols = array_img_2d.arraySort(keys_2d_cols)\nprint('2D array image sorted cols (pixel):',\n samp_arr_img(array_img_2d_sorted_cols).getInfo())\n# [[2, 9, 8, 3],\n# [4, 7, 11, 1],\n# [0, 10, 6, 5]]\n```"]]