Skip to content

lsort ​

Sorts a list and returns the sorted result.

Syntax ​

tcl
lsort ?options? list

Parameters ​

  • list: The list to sort

Options ​

Sort type ​

  • -ascii: Dictionary-style string comparison (default)
  • -dictionary: Natural sort that handles embedded numbers (e.g., "a2" before "a10")
  • -integer: Compare as integers
  • -real: Compare as floating-point numbers

Sort order ​

  • -increasing: Ascending order (default)
  • -decreasing: Descending order

Result control ​

  • -indices: Return the sorted indices instead of the sorted values
  • -unique: Remove duplicate values from the result

Sub-list sorting ​

  • -index indexSpec: Sort by a specific element within each sub-list. The indexSpec can be a number (0-based), end, end-N, or a list of indices for nested access
  • -stride length: Treat the list as groups of length elements, sorting by the first element of each group. Length must be at least 2

Custom comparison ​

  • -command cmdName: Use a custom comparison procedure. The procedure must take two arguments and return a negative integer, zero, or positive integer

Modifiers ​

  • -nocase: Case-insensitive comparison (works with -ascii and -dictionary)

Examples ​

Sort strings alphabetically ​

Output

Sort numbers as integers ​

Output

Sort in descending order ​

Output

Case-insensitive sort ​

Output

Remove duplicates ​

Output

Sort floating-point numbers ​

Output

Combine options ​

Output

Dictionary sort with embedded numbers ​

Output

Sort by sub-list element ​

Output

Sort by sub-list element as integers ​

Output

Get sorted indices ​

Output

Sort with stride (key-value pairs) ​

Output

Sort stride pairs by value ​

Output

Custom comparison command ​

Output

See Also ​