Skip to main content

Sort Table

Sorts a DataTable by a specified column in ascending or descending order. The entire table is reordered based on the values in the sort column.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Input

  • Table - DataTable to sort
  • Column Name - Column to sort by

Output

  • Table - Sorted DataTable with rows reordered based on the sort column

Options

  • Direction - Sort direction
    • Ascending (ASC) - Sort from lowest to highest (A-Z, 0-9)
    • Descending (DESC) - Sort from highest to lowest (Z-A, 9-0)

Example Usage

Sort by Name Alphabetically

// Table: ["ID", "Name", "Age", "Email"]
// Unsorted:
// Row 0: ID=3, Name="Charlie", Age=35
// Row 1: ID=1, Name="Alice", Age=28
// Row 2: ID=2, Name="Bob", Age=30

var columnName = "Name";
var direction = "ASC"; // Ascending

// Result:
// Row 0: ID=1, Name="Alice", Age=28
// Row 1: ID=2, Name="Bob", Age=30
// Row 2: ID=3, Name="Charlie", Age=35

Sort by Age Descending

// Sort from oldest to youngest
var columnName = "Age";
var direction = "DESC"; // Descending

// Result:
// Row 0: ID=3, Name="Charlie", Age=35
// Row 1: ID=2, Name="Bob", Age=30
// Row 2: ID=1, Name="Alice", Age=28

Sort by Price Ascending

// Product table: ["ProductID", "Name", "Price", "Stock"]
var columnName = "Price";
var direction = "ASC";

// Result: Products sorted from cheapest to most expensive

Sort by Date

// Order history: ["OrderID", "CustomerName", "OrderDate", "Amount"]
var columnName = "OrderDate";
var direction = "DESC"; // Most recent first

// Result: Orders sorted from newest to oldest

Multi-Step Sorting

// Primary sort by category, then by price
// Step 1: Sort by Price
var columnName = "Price";
var direction = "ASC";
var table1 = /* Sort Table output */;

// Step 2: Sort by Category (stable sort maintains price order within categories)
var columnName = "Category";
var direction = "ASC";
var table2 = /* Sort Table output */;

Sort Before Processing

// Sort employees by salary before processing bonuses
var columnName = "Salary";
var direction = "DESC"; // Highest first

var sortedTable = /* Sort Table output */;

// Process from highest to lowest salary
for (var i = 0; i < 10; i++) { // Top 10 earners
var row = /* Get Row i from sortedTable */;
// Award bonus
}

Sort Before Export

// Sort data before creating a report
var columnName = "Department";
var direction = "ASC";
var sortedByDept = /* Sort Table output */;

// Then sort by name within departments
var columnName = "Name";
var direction = "ASC";
var finalTable = /* Sort Table output */;

// Export to CSV or Excel

Sorting Different Data Types

Numeric Sorting

// Numbers sort numerically
// Column: [100, 20, 3, 40, 5]
// ASC: [3, 5, 20, 40, 100]
// DESC: [100, 40, 20, 5, 3]

var columnName = "Quantity";
var direction = "ASC";

String Sorting

// Strings sort alphabetically (case-sensitive)
// Column: ["Zebra", "apple", "Banana", "cherry"]
// ASC: ["Banana", "Zebra", "apple", "cherry"] // Capital letters first
// DESC: ["cherry", "apple", "Zebra", "Banana"]

var columnName = "ProductName";
var direction = "ASC";

Date Sorting

// Dates sort chronologically
// Column: ["2024-03-15", "2023-12-01", "2024-01-20"]
// ASC: ["2023-12-01", "2024-01-20", "2024-03-15"]
// DESC: ["2024-03-15", "2024-01-20", "2023-12-01"]

var columnName = "CreatedDate";
var direction = "ASC";

Tips

  • Sorting reorders all rows based on the specified column
  • The sort is stable - rows with equal values maintain their relative order
  • Column names are case-sensitive
  • Numbers sort numerically, strings sort alphabetically
  • Empty or null values typically sort to the beginning (ASC) or end (DESC)
  • For multi-column sorting, apply Sort Table multiple times
  • The original table is not modified; a new sorted table is returned
  • Sort before filtering for better performance on large datasets

Common Errors

Empty Table Error

Error: Table cannot be empty. Please provide a valid DataTable.

Solution: Ensure you're passing a valid DataTable object.

// Wrong
var table = null;

// Correct
var table = /* Valid DataTable */;

Empty Column Name Error

Error: Column name cannot be empty. Please provide a valid column name.

Solution: Provide a valid column name.

// Wrong
var columnName = "";

// Correct
var columnName = "Price";

No Direction Selected Error

Error: Sort direction must be selected. Please choose ASC or DESC.

Solution: Select either ASC or DESC in the Direction option.

Make sure to select a direction in the node properties:
- Direction: Ascending (ASC)
or
- Direction: Descending (DESC)

Column Not Found Error

If the specified column doesn't exist in the table, you'll get an error.

// Table columns: ["ID", "Name", "Email"]

// Wrong - column doesn't exist
var columnName = "Age"; // Error!

// Correct
var columnName = "Name";

Solution: Verify the column name exists and is spelled correctly (case-sensitive).

Advanced Examples

Top N After Sorting

// Get top 5 products by sales
var columnName = "TotalSales";
var direction = "DESC";
var sortedTable = /* Sort Table output */;

// Get top 5
var top5 = [];
for (var i = 0; i < 5; i++) {
var row = /* Get Row i */;
top5.push(row);
}

Sorting for Rank Assignment

// Sort by score descending
var columnName = "Score";
var direction = "DESC";
var sortedTable = /* Sort Table output */;

// Assign ranks
var rowCount = sortedTable.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = /* Get Row i */;
row["Rank"] = i + 1; // Rank 1, 2, 3, etc.
// Update row or add to new table
}

Conditional Sorting

// Sort by different columns based on user preference
var sortColumn = /* user selection: "Name", "Price", "Date" */;
var sortDirection = /* user selection: "ASC", "DESC" */;

var columnName = sortColumn;
var direction = sortDirection;

var sortedTable = /* Sort Table output */;

Sorting Merged Data

// Merge multiple tables
var mergedTable = /* Merge DataTable output */;

// Sort the merged result
var columnName = "Timestamp";
var direction = "ASC";
var sortedMerged = /* Sort Table output */;

// Now you have chronologically ordered data from multiple sources

See Also