Nodes/comfy-ovum/_.sortBy
ComfyUI Node

_.sortBy

Sort a list by a property, a length, or a custom key

By sfinktah·Created about a year ago·Updated 10 months ago· 7
_.sortBy
  • list_or_dict
  • iteratee
  • *
iteratee_json

_.sortBy is the workhorse sorter: it returns a stably sorted copy of your list, ranked in ascending order by the result of running each value through an iteratee. The iteratee can be a property name - sort a list of strings by "length", shortest first - or a key selector, or (via the JSON widget) a function expressed as data. Stable means ties keep their original order, which matters more than people think when you're sorting on one field and don't want the others jumbled.

It's the "sort by something" version of the family's plain _.sort (which sorts values directly), and by far the one you'll actually want. This is one of the more complete members of the ovum/underscore family in comfy-ovum - auto-generated wrappers around underscore3, the bundled Python port of Underscore.js - because it's a proper iteratee method, so it got the full predicate treatment. The family's author's warning still applies (WIP, not recommended for production), but the rough edges here are manageable.

Why you'd reach for it

Sorting a list of filenames by length or alphabetically before a batch op. Ordering prompt variants by a score attribute so the best lands first. Sorting captions by character count. Any "put these in a sensible order based on a property of each item" task. Combined with _.take (first n) you get "top n by a criterion," which is a pattern you'll rebuild constantly.

Inputs that matter

  • list_or_dict (*) - the collection to sort.
  • iteratee (*) - the ANY input socket for the key function or selector, if you're wiring one in from elsewhere.
  • iteratee_json (STRING, multiline) - the widget-only fallback used when iteratee isn't connected. This is the one you'll type in. A property name like length works directly; JSON like {"key": "score"} or an expression the library can interpret resolves through underscore's iteratee machinery.

Output is a single * carrying the sorted list. Chain behavior applies: pass a _.CHAIN in as the primary input and it returns a chain - unwrap with _.value.

Install

# via ComfyUI Manager: search "comfy-ovum"
cd ComfyUI/custom_nodes
git clone https://github.com/sfinktah/comfy-ovum
# restart ComfyUI

No model downloads; pure Python, deps (aiohttp, pillow, numpy, requests, etc.) via Manager, underscore3 bundled. If your sort silently returns the input order, the iteratee JSON failed to parse - the wrapper falls back to no-op rather than crashing, which is kind but confusing. Check the JSON in the widget.

Categoryovum/underscore

Inputs (3)

NameTypeDefaultDescription
list_or_dictopt*Primary input object (expected collection). You can still pass any JSON-serializable value. Also accepts _.CHAIN to continue chaining.
iterateeopt*iteratee: ANY input to override widget. Accepts function, key string, or object shorthand.
iteratee_jsonoptSTRINGiteratee_json: JSON or key selector. Used when 'iteratee' input is not connected.

Outputs (1)

NameTypeDescription
**