n8n AI input ordering: Sort modes and numeric pitfalls
Define field types, distinguish Simple, Random and Code, and check that reordered records remain complete.
Define order rather than inferring priority · Select Simple, Random or Code · Default JavaScript sort converts to strings
At a glance
The goal is to send records to a later AI step in an explicit field order. You need known fields and an editable workflow. The Sort documentation was checked and string/numeric ordering modeled locally. The node was not run; model reading order and semantic priority were not verified.
Input, configuration and output checks
Define input
Inspect identifiers and field types.
Configure the operation
Choose the documented mode and relevant options.
Inspect results
Check mapping and output before downstream use.
Define order rather than inferring priority
Synthetic input is a/score=2, b/score=10 and c/score=3, retaining ids and text. Specify numeric ascending or string lexicographic order and inspect the upstream type. Field ordering does not establish content importance or guarantee that date strings order by actual instants. Missing fields, mixed types and ties need separate node checks; this limited fixture does not define them.
Select Simple, Random or Code
Simple orders chosen fields ascending or descending; Random randomizes; Code uses custom JavaScript. For a fixed field order, inspect the Simple field, direction and dot notation setting. A nested path differs from a literal dotted key. Random cannot establish stable order. Code requires checking the comparator and input rather than assuming automatic inference.
Default JavaScript sort converts to strings
The docs warn that default JavaScript sort converts values to strings before comparison. Locally, [2,10,3] sorts to [10,2,3] by default and [2,3,10] with (a,b)=>a-b. This is independent JavaScript behavior, not proof of the Simple implementation. For Code mode, access values through the actual item structure and validate types; a numeric-array comparator is not complete node code.
Verify ordering and retained records
Local checks covered the two orderings and id membership, not missing values, ties or node scheduling. Execute a small sample and inspect direction, ids and retained text, then confirm that the downstream step consumes that sequence. Stop later model calls on a mismatch and inspect types, mode and dot notation. Sorting after model calls cannot change input order of requests already made.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.