Skip to main content

Try it Live

Run Address examples in the interactive playground
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
View the complete executable example at playground/src/examples/primitives/address/compare.ts.

C API - Manual Implementation Required

The C API currently provides primitives_address_equals() but not a three-way comparison. Implement lexicographic comparison manually:Example:
Alternative: Use memcmp for byte-level comparison:
See also: primitives_address_equals() for equality comparison

Comparison Semantics

Lexicographic ordering: Compares byte-by-byte from left to right (big-endian). Return values:
  • -1: First address is less than second
  • 0: Addresses are equal
  • 1: First address is greater than second
Example ordering:

Use Cases

Sorting Arrays

Sort addresses in ascending or descending order:
Efficient lookup in sorted arrays:

Ordered Sets

Maintain addresses in sorted order:

Range Queries

Find addresses within a range:

Merkle Trees

Build ordered merkle trees using address ordering:

Performance

Time complexity: O(n) where n = 20 bytes (constant time for addresses) Implementation: Compares byte-by-byte until difference found or end reached Early termination: Stops at first differing byte For large-scale sorting, consider converting to hex once:

Relation to Other Comparisons

Helper methods built on compare():

Sorting Examples

Sort transaction list by sender:

Group addresses by range:

See Also