Nodes/comfy-ovum/_.sortedIndex
ComfyUI Node

_.sortedIndex

Where would this value slot into a sorted list?

By sfinktah·Created about a year ago·Updated 10 months ago· 7
_.sortedIndex
  • py_list
  • value
  • iteratee
  • INT
iteratee_json

_.sortedIndex answers one precise question: if this list is already sorted, at what index would a given value slot in to keep it sorted? Given [10, 20, 30] and a value of 25, the answer is 2 - it belongs between 20 and 30. Under the hood it's a binary search, so it runs in O(log n) instead of scanning everything, and if you provide an iteratee, the search uses that to compute the "rank" of each element rather than raw values.

This is a niche, mostly-programmer node. You won't reach for it every day. Where it actually earns its keep: maintaining sorted lists incrementally (insert a new item at the position it belongs instead of re-sorting), finding a threshold index ("how many items are below this cutoff"), or computing how many elements in a sorted score list are worse than a candidate. If none of that sounds like your workflow, this is the underscore node you can safely skip.

It's part of the ovum/underscore family in comfy-ovum - auto-generated wrappers around underscore3, the bundled Python port of Underscore.js. The author's README flags the whole family as a work in progress, not for production. For a binary-search utility that's mostly a codegen exercise anyway, treat this as a curiosity until you have a concrete need.

Inputs and output

  • py_list (*) - the already-sorted array. Note the label: the generator names array-type primary inputs py_list.
  • value (*) - the value you're placing. JSON allowed for arrays/objects where applicable.
  • iteratee (*) - optional ANY socket for a key function/selector, if you want to rank by a property instead of raw value.
  • iteratee_json (STRING, multiline) - widget-only fallback for the iteratee when the socket isn't connected; a property name like length works.

Output is an INT - the insertion index, in range 0..len(list). The binary search mirrors the standard Underscore behavior, returning the smallest index that maintains order (so ties insert after the existing equal values... actually before the first greater element, exactly where the element would sit).

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 in-repo. The one real gotcha: the node assumes your input is already sorted. Feed it an unsorted list and the answer is meaningless - garbage-in, confident-garbage-out. If you're not certain the list is sorted, run _.sortBy first.

Categoryovum/underscore

Inputs (4)

NameTypeDefaultDescription
py_listopt*Primary input object (expected array). You can still pass any JSON-serializable value. Also accepts _.CHAIN to continue chaining.
valueopt*value: JSON allowed for arrays/objects where applicable.
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
INTINT