There are two contexts.
-
The context passed to the lessThanFn.
Context is an extra user defined argument passed fromsort
to thelessThanFn
.
Most of the times Context is not used (setvoid
as type and{}
as value).
It is used instd.mem.sort
and instd.mem.sortUnstable
.
Example: Sorting Strings in Zig -
The context struct for comparing and swapping:
Context is a namespace providing two functions:pub fn lessThan(ctx: Context, a: usize, b: usize) bool {} pub fn swap(ctx: Context, a: usize, b: usize) void {}
This is used with the sortContext functions:
std.sort.insertionContext
,std.sort.heapContext
,std.sort.pdqContext
,std.mem.sortContext
andstd.mem.sortUnstableContext
.
In thelessThan
andswap
functions, the context argument is the same context type that is used as namespace.
@Mambe welcome to ziggit