Ask AI
Skip to main content

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: ARRAY of VARIABLE, 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: ARRAY of OBJECT, 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).

Output

  • result (Type: ARRAY of VARIABLE): 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)
  • Result: The RESULT variable 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
  • Result: The RESULT variable 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
  • Result: The RESULT variable will contain the customer list sorted by lastName in 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
      • Rule 2:
        • Attribute: dueDate
        • Order: Ascending
  • Result: The RESULT variable will contain the tasks sorted first by priority (highest first), then by dueDate (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"\}
    ]