ComfyUI Node

sort

Sort a list, ascending or descending, in one node

By StableLlama·Created about a year ago·Updated 4 days ago· 48
sort
  • list
  • list
reverseFalse

Lists get messy, and sometimes you want to see them in order. DataListSort takes a list and returns a new list with the items sorted - ascending by default, descending if you flip a switch. It's Python's sorted() as a node, which means you get stable, predictable ordering.

Inputs: list (any type) and a reverse toggle with two choices, False and True (default False). Output: the sorted list.

How it works

The node calls sorted() on the input - it doesn't touch your original list, so re-runs are consistent. The reverse option is a string dropdown ("False"/"True"), which is one of those slightly awkward pack conventions, but the behavior is what matters: leave it on False for ascending, flip to True for descending.

A couple of Python-sorting realities worth knowing:

  • Numbers sort numerically, strings sort alphabetically. Lists of mixed types will throw an error, so keep a list homogeneous - if you sorted ints and floats together, they compare fine; ints and strings won't.
  • It's a stable sort, meaning equal elements keep their original relative order. For most ComfyUI uses that's a non-issue, but it's good to know it's not shuffling ties.

Where it fits

Sorting shows up in unglamorous but real places: ordering a batch of scores before slicing off the top N, arranging prompts into a consistent order before iteration, or just making a messy candidate list readable while you debug. For finding the single best or worst value, DataListMax/Min are lighter tools - sort is for when you want the whole ordering. Descending sort plus a slice is the classic "top 5 of this list" move, all with pack nodes.

Installing it

Part of StableLlama's Basic data handling pack - pure Python, zero dependencies, no model downloads:

cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling

Restart ComfyUI, or search "Basic data handling" in ComfyUI Manager. No requirements.txt conflict potential, because the pack ships with no third-party dependencies.

Common issues

The mixed-type error is the main trap - sort a list that contains both numbers and strings and the node will fail loudly. Keep lists homogeneous and you're fine. And remember the output is a brand-new sorted list, not a mutation of the input; if you expected the original connection to also be sorted, it won't be - that's by design and usually what you want.

CategoryBasic/Data List

Inputs (2)

NameTypeDefaultDescription
list*
reverseoptCOMBOFalse2 options: False, True

Outputs (1)

NameTypeDescription
list*