PD文本列表排序
PD文本列表排序 fixes file_10-before-file_2, the one sort people actually need
- text_list
- filename_text
- sort_info
The sort that keeps your captions glued to their filenames
PD_TextListSort ("PD文本列表排序") exists because "sort the list" is never just sorting the list. You have a batch of captions and a parallel set of filenames - from a folder loader or the pack's PD_TextListPack - and if you reorder one without the other, text and filename quietly fall out of sync. This node takes text_list and filename_text, sorts them together by filename, and returns both reordered, plus a sort_info string telling you what it did.
It's the kind of data-plumbing work the KB's comfyui-node-plumbing.md describes: a node that touches no pixels but keeps a workflow legible and correct. If you're preparing a training set where captions must line up with images, this is the node that keeps the pairing honest.
How it works
The mechanism is plain and solid. It splits filename_text on newlines, zips the filenames with the text list, checks the two lengths match (truncating to the smaller if they don't), then sorts by the filename using a key function chosen by sort_mode. The interesting one is natural sort, which splits filenames into digit and non-digit chunks and compares the digits numerically - that's what makes file_2 sort before file_10 instead of the alphabetical file_10 first. Date sort tries four common date patterns (2024-01-15, 20240115, 15-01-2024, 24-01-15) and falls back to a very old date for unparseable names.
Inputs and outputs
text_list- the captions/contents (list-capable).filename_text- newline-joined filenames, the thing the sort keys on.sort_mode- eight options: alphabetical, numeric, natural, and date, each in ascending or descending order. Default is 自然排序(正序) - natural, ascending, which is the one you want 90% of the time.- Outputs:
text_list,filename_text(both reordered to match), andsort_info- a short human-readable report of mode, count and the first three items before/after. Wire it to Show Text.
Installing it
Part of Comfyui_PDuse:
cd ComfyUI/custom_nodes
git clone https://github.com/7BEII/Comfyui_PDuse.git
cd Comfyui_PDuse
pip install -r requirements.txt
Or search "Comfyui_PDuse" in ComfyUI Manager and restart.
Where it bites
Mismatched lengths are handled by silent truncation - if the filename list is longer or shorter than the text list, the node keeps the smaller count and warns in the console, so captions can quietly vanish from the output. Don't ignore that warning. Also, if filename_text is empty the node returns an error string in sort_info and a [""] list rather than crashing - which is polite but easy to miss downstream. The sort keys on filenames only, never on the text itself, so if your filenames don't encode the order you want (numbers, dates), you'll get nonsense in, nonsense out. Keep filenames consistent and this is the un-dramatic utility that makes batch captioning actually work.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| text_list | STRING | — | |
| filename_text | STRING | — | |
| sort_mode | COMBO | 自然排序(正序) | 8 options: 字母顺序(正序), 字母顺序(倒序), 数字顺序(正序), 数字顺序(倒序), 自然排序(正序), 自然排序(倒序), +2 |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| text_list | STRING | — |
| filename_text | STRING | — |
| sort_info | STRING | — |