cv2.sort
Ordering Rows and Columns Without Leaving the Graph
- src
- nparray
Sorting a matrix sounds like it belongs in pandas, not ComfyUI - until you have a row of match scores, a column of response values, or a per-frame measurement table sitting in the graph as an NPARRAY and you want them in order here, not after a round trip through a file. cv2.sort is the array-level sort in ComfyUI CV (bmad4ever/comfyui_cv), and its sibling cv2.sortIdx gives you the same ordering as indices.
The node is one function, two inputs, one output. The only thing to understand is the flags dropdown, because it decides what gets sorted.
Inputs
src- an NPARRAY. No IMAGE or MASK link is accepted: this is a data array, and single-channel data at that, so an RGB image has no meaningful sorting here. Bridge first with Image → CV Array and drop to one channel if you really do want to sort pixel values.flags- one of four combinations:SORT_EVERY_ROW | SORT_ASCENDING(the default)SORT_EVERY_ROW | SORT_DESCENDINGSORT_EVERY_COLUMN | SORT_ASCENDINGSORT_EVERY_COLUMN | SORT_DESCENDING
The first half picks the axis - each row sorted independently, or each column - and the second half the direction. It's rendered as a single combo because the OpenCV flag is a bitfield, so "row ascending" is one choice, not two.
One output: nparray, the sorted array, same shape as the input. Sorting is per-line: a 5x20 array of scores comes back as 5x20 with each row in order, not twenty rows globally ordered. If you wanted a flat global sort, that's CV Reshape Array first.
Where it earns its place
Sorting is the setup step for everything that's "the top few of these": take the k strongest responses along a scanline, pick the best N of a per-row score table, order a histogram before trimming the tails, put a set of measured values in order before taking a percentile with CV Array Statistic. Keep the pairs in mind - sort reorders values, sortIdx tells you where they came from - and remember that if all you want is the single best element and its location, cv2.minMaxLoc returns the value and the position in one node and is far less work.
Install
Manager → search ComfyUI CV → install, or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Restart ComfyUI afterwards. Python ≥ 3.12 and a recent V3-API ComfyUI. No models, no downloads, no GPU work - this runs in microseconds.
Where people get burned
Wrong axis, no warning. Sorting columns when you meant rows produces a perfectly valid array that is not the one you wanted. It's the failure mode this node shares with every axis-parameterised function ever written: print shapes with Inspect CV Data and check.
Losing the association. After sort, element 0 of a row is the smallest value - but you no longer know which column it came from. Once you drop indices, a sorted score list can't be joined back to the thing it scored. If that link matters, use cv2.sortIdx.
Unsigned pixel data. For a uint8 array, "ascending" on 0–255 values is intuitive; for signed or float data, negative values sort below everything, which people forget when the array came out of a difference or a gradient.
Contrib-wheel collisions. All four OpenCV distributions share one site-packages/cv2, so a non-contrib wheel installed over the contrib build leaves contrib-backed nodes missing from the menu with no error in the log. tools/repair_opencv_contrib.py --check detects it; --apply fixes it.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | input single-channel array. A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. | |
| flags | COMBO | SORT_EVERY_ROW | SORT_ASCENDING | operation flags, a combination of #SortFlags |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |