There are two contexts.
-
The context passed to the lessThanFn.
Context is an extra user defined argument passed fromsortto thelessThanFn.
Most of the times Context is not used (setvoidas type and{}as value).
It is used instd.mem.sortand 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.sortContextandstd.mem.sortUnstableContext.
In thelessThanandswapfunctions, the context argument is the same context type that is used as namespace.
@Mambe welcome to ziggit ![]()