Sort list
Function: Sort list
This function allows you to reorder a list of items based on specific criteria. You can sort lists of simple values like numbers or text, or more complex lists of objects by one or more of their attributes. This is useful for organizing data in a meaningful way, such as displaying customer records alphabetically or product prices from highest to lowest.
Input
- list (Type:
ARRAYofVARIABLE, Required): The list of items (numbers, text, or objects) you want to sort. This could be a list of customer names, product prices, or a collection of customer records. - Sort by (Type:
ARRAYofOBJECT, Optional): This is where you define the rules for how your list should be sorted. You can add multiple sorting rules to sort by different attributes in a specific order.- Attribute (Type:
DATA_FORMAT_ATTRIBUTE): If your list contains objects (like customer records), select the specific attribute (e.g., "name", "price", "date") within those objects that you want to use for sorting. If your list contains simple values (like just numbers or text), this option is not needed. - Order (Type:
SELECT_ONE): Choose the sorting direction:- Ascending: Sorts from smallest to largest (e.g., A-Z, 1-100, oldest to newest).
- Descending: Sorts from largest to smallest (e.g., Z-A, 100-1, newest to oldest). If no sorting rules are provided, the list will be sorted in descending order by its natural value (e.g., numbers from high to low, text alphabetically Z-A).
- Attribute (Type:
Output
- result (Type:
ARRAYofVARIABLE): The newly sorted list. This output will contain all the original items, but arranged according to your specified sorting rules. By default, this sorted list will be stored in a variable named "RESULT".
Execution Flow
Real-Life Examples
Example 1: Sorting a list of numbers from highest to lowest
Imagine you have a list of sales figures and you want to quickly see the highest sales first.
- Inputs:
- list:
[150, 75, 300, 200, 50] - Sort by: (Leave empty, as the default behavior for simple lists is descending)
- list:
- Result:
The
RESULTvariable will contain the list sorted from highest to lowest:[300, 200, 150, 75, 50].
Example 2: Sorting a list of product names alphabetically
You have a list of product names and want to display them in alphabetical order on your website.
- Inputs:
- list:
["Laptop", "Mouse", "Keyboard", "Monitor", "Webcam"] - Sort by:
- Attribute: (Not applicable for simple text lists)
- Order:
Ascending
- list:
- Result:
The
RESULTvariable will contain the list sorted alphabetically:["Keyboard", "Laptop", "Monitor", "Mouse", "Webcam"].
Example 3: Sorting a list of customer records by last name (ascending)
You have a list of customer records, each containing a first name and last name, and you want to organize them by last name.
- Inputs:
- list:
[
\{"firstName": "Alice", "lastName": "Smith"\},
\{"firstName": "Bob", "lastName": "Johnson"\},
\{"firstName": "Charlie", "lastName": "Brown"\}
] - Sort by:
- Attribute:
lastName - Order:
Ascending
- Attribute:
- list:
- Result:
The
RESULTvariable will contain the customer list sorted bylastNamein ascending order:[
\{"firstName": "Charlie", "lastName": "Brown"\},
\{"firstName": "Bob", "lastName": "Johnson"\},
\{"firstName": "Alice", "lastName": "Smith"\}
]
Example 4: Sorting a list of tasks by priority (descending) then by due date (ascending)
You have a list of tasks with a priority level (e.g., 1 for low, 5 for high) and a due date. You want to see the highest priority tasks first, and for tasks with the same priority, you want to see the ones due sooner.
- Inputs:
- list:
[
\{"taskName": "Review Report", "priority": 3, "dueDate": "2023-11-15"\},
\{"taskName": "Send Email", "priority": 5, "dueDate": "2023-11-10"\},
\{"taskName": "Schedule Meeting", "priority": 3, "dueDate": "2023-11-12"\},
\{"taskName": "Prepare Presentation", "priority": 5, "dueDate": "2023-11-12"\}
] - Sort by:
- Rule 1:
- Attribute:
priority - Order:
Descending
- Attribute:
- Rule 2:
- Attribute:
dueDate - Order:
Ascending
- Attribute:
- Rule 1:
- list:
- Result:
The
RESULTvariable will contain the tasks sorted first bypriority(highest first), then bydueDate(earliest first for same priority):[
\{"taskName": "Send Email", "priority": 5, "dueDate": "2023-11-10"\},
\{"taskName": "Prepare Presentation", "priority": 5, "dueDate": "2023-11-12"\},
\{"taskName": "Schedule Meeting", "priority": 3, "dueDate": "2023-11-12"\},
\{"taskName": "Review Report", "priority": 3, "dueDate": "2023-11-15"\}
]