sort_key

Converts INT256/INT512/UINT256/UINT512 to a sortable BLOB for use in ORDER BY. Necessary because Determica's default BLOB ordering doesn't handle signed integers correctly. Use sort_key only inside ORDER BY or comparison-key workflows; it is not a human-readable number conversion, hash, or portable serialization.

Example

Local

1
-- Sort signed accounting deltas numerically
2
WITH pnl(label, delta) AS (
3
VALUES
4
('loss', '-2500000'::INT256),
5
('flat', '0'::INT256),
6
('profit', '1750000'::INT256)
7
)
8
SELECT label, delta::VARCHAR AS raw_delta
9
FROM pnl
10
ORDER BY sort_key(delta);
11
-- => [{"label":"loss","raw_delta":"-2500000"},{"label":"flat","raw_delta":"0"},{"label":"profit","raw_delta":"1750000"}]
Notebook ready in readonly mode.

API reference

Exact signatures with descriptions, requirements, inputs, returns, and examples.

sort_key(INT256) #

Builds a sortable key for signed INT256 values. The returned BLOB is for ordering, not display.

Inputs

Name Type Use
value INT256

INT256, INT512, UINT256, or UINT512 value to turn into an ordering key.

required positional

Returns

Name Type
sortable_key BLOB

Sortable binary key. Returns a BLOB whose byte ordering matches numeric ordering. It is not a display or hash format.

Guidance

Ordering wide integers

Use sort_key inside ORDER BY when signed or wide EVM integers must sort numerically. The returned BLOB is an implementation detail for ordering.

  • Do not display sort_key output to users.
  • Use format_units or format_ether when you need a human-readable amount.

Local SQL

1
WITH balances(raw_amount) AS (
2
VALUES (parse_units('2500', 6)), (parse_units('125', 6))
3
)
4
SELECT raw_amount
5
FROM balances
6
ORDER BY sort_key(raw_amount) DESC;
Notebook ready in readonly mode.

Additional overloads

sort_key(INT512) #

Builds a sortable key for signed INT512 values. The returned BLOB is for ordering, not display.

Inputs

Name Type Use
value INT512

INT256, INT512, UINT256, or UINT512 value to turn into an ordering key.

required positional

Returns

Name Type
sortable_key BLOB

Sortable binary key. Returns a BLOB whose byte ordering matches numeric ordering. It is not a display or hash format.

sort_key(UINT256) #

Builds a sortable key for unsigned UINT256 values. The returned BLOB is for ordering, not display.

Inputs

Name Type Use
value UINT256

INT256, INT512, UINT256, or UINT512 value to turn into an ordering key.

required positional

Returns

Name Type
sortable_key BLOB

Sortable binary key. Returns a BLOB whose byte ordering matches numeric ordering. It is not a display or hash format.

sort_key(UINT512) #

Builds a sortable key for unsigned UINT512 values. The returned BLOB is for ordering, not display.

Inputs

Name Type Use
value UINT512

INT256, INT512, UINT256, or UINT512 value to turn into an ordering key.

required positional

Returns

Name Type
sortable_key BLOB

Sortable binary key. Returns a BLOB whose byte ordering matches numeric ordering. It is not a display or hash format.

Examples

Local SQL

1
-- Sort balances wider than native 128-bit integers
2
WITH balances(label, raw_balance) AS (
3
VALUES
4
('small', 100::UINT256),
5
('whale', '340282366920938463463374607431768211456'::UINT256),
6
('medium', 1000000::UINT256)
7
)
8
SELECT label, raw_balance::VARCHAR AS raw_balance
9
FROM balances
10
ORDER BY sort_key(raw_balance) DESC;
11
-- => [{"label":"whale","raw_balance":"340282366920938463463374607431768211456"},{"label":"medium","raw_balance":"1000000"},{"label":"small","raw_balance":"100"}]
Notebook ready in readonly mode.

Related functions

Category and tags

Category
EVM utilities
Tag
Types
Tag
EVM