Skip to content

Commit

Permalink
Implement sort
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed May 26, 2024
1 parent 23bcef3 commit b9e3052
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/processes/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import GeeProcess from '../processgraph/process.js';

export default class sort extends GeeProcess {

executeSync(node) {
const ee = node.ee;
let data = node.getArgumentAsEE("data");
const value = node.getArgument("asc", true);

if (data instanceof ee.List) {
data = data.sort();
if (!value) {
data = data.reverse();
}
return data;
}
else if (data instanceof ee.Array) {
data = data.sort();
if (!value) {
// todo: how to sort in descending order?
throw node.invalidArgument("asc", "Data of type ee.Array can't be sorted in descending order.");
}
return data.sort();
}

throw node.invalidArgument("data", "Unsupported data type.");
}
}
73 changes: 73 additions & 0 deletions src/processes/sort.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"id": "sort",
"summary": "Sort data",
"description": "Sorts an array into ascending (default) or descending order.\n\n**Remarks:**\n\n* The ordering of ties is implementation-dependent.\n* Temporal strings can *not* be compared based on their string representation due to the time zone/time-offset representations.",
"categories": [
"arrays",
"sorting"
],
"parameters": [
{
"name": "data",
"description": "An array with data to sort.",
"schema": {
"type": "array",
"items": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
},
{
"type": "string",
"format": "date-time",
"subtype": "date-time"
},
{
"type": "string",
"format": "date",
"subtype": "date"
}
]
}
}
},
{
"name": "asc",
"description": "The default sort order is ascending, with smallest values first. To sort in reverse (descending) order, set this parameter to `false`.",
"schema": {
"type": "boolean"
},
"default": true,
"optional": true
}
],
"returns": {
"description": "The sorted array.",
"schema": {
"type": "array",
"items": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
},
{
"type": "string",
"format": "date-time",
"subtype": "date-time"
},
{
"type": "string",
"format": "date",
"subtype": "date"
}
]
}
}
}
}

0 comments on commit b9e3052

Please sign in to comment.