Lexicographical comparisons

Are there any QOL functions for tuple-like lexicographical comparisons? Consider the following C++ code:

struct Some_Render_Layer {
    int id;
    int list_id;
};

... elsewhere in code ...

bool less_than = 
        std::tie(render_layer1.id,  render_layer1.list_id) < std::tie(render_layer2.id,  render_layer2.list_id);

Obviously, I’m aware you can implement this kind lexicographical comparison without std::tie-like facility, but this is convenient. I didn’t find anything in the docs. :frowning:

Short answer is no.

If you want such a thing, you’d have to implement it yourself.

import "core:slice"

Some_Render_Layer :: struct {
    id:      i32,
    list_id: i32,
}

some_render_layer_cmp :: proc(a, b: Some_Render_Layer) -> slice.Ordering {
    id := slice.cmp(a.id, b.id)
    if id != .Equal {
        return id
    }
    return slice.cmp(a.list_id, b.list_id)
}

// or even this:

some_render_layer_i64_cmp :: proc(a, b: Some_Render_Layer) -> slice.Ordering {
    return slice.cmp(transmute(i64)a, transmute(i64)b)
}
3 Likes