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.

Scalar function TypesEVM

Example

Runs locally

-- Sort signed accounting deltas numerically
WITH pnl(label, delta) AS (
  VALUES
    ('loss', '-2500000'::INT256),
    ('flat', '0'::INT256),
    ('profit', '1750000'::INT256)
)
SELECT label, delta::VARCHAR AS raw_delta
FROM pnl
ORDER BY sort_key(delta);
-- => [{"label":"loss","raw_delta":"-2500000"},{"label":"flat","raw_delta":"0"},{"label":"profit","raw_delta":"1750000"}]

API reference

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.

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.

Overload examples

Runs locally

SELECT value::VARCHAR FROM (VALUES ('-2'::INT512), (1::INT512)) t(value) ORDER BY sort_key(value);
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.

Overload examples

Runs locally

SELECT value::VARCHAR FROM (VALUES (2::UINT512), (1::UINT512)) t(value) ORDER BY sort_key(value);

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.

Runs locally

WITH balances(raw_amount) AS (
  VALUES (parse_units('2500', 6)), (parse_units('125', 6))
)
SELECT raw_amount
FROM balances
ORDER BY sort_key(raw_amount) DESC;

Examples

Runs locally

-- Sort balances wider than native 128-bit integers
WITH balances(label, raw_balance) AS (
  VALUES
    ('small', 100::UINT256),
    ('whale', '340282366920938463463374607431768211456'::UINT256),
    ('medium', 1000000::UINT256)
)
SELECT label, raw_balance::VARCHAR AS raw_balance
FROM balances
ORDER BY sort_key(raw_balance) DESC;
-- => [{"label":"whale","raw_balance":"340282366920938463463374607431768211456"},{"label":"medium","raw_balance":"1000000"},{"label":"small","raw_balance":"100"}]