0

sort() can be used on lists of integers without further ado.

But if I define my own type, which trait shall I implement on it so that lists made of it can be sorted?

@value
struct MyStruct(LessThanOrEqualComparable):
    fn __le__(self, other: Self) -> Bool:
        return True

fn main() raises:
    var my_integers = List(3,1,2)
    sort(my_integers)
    print(" ".join(my_integers))

    var my_objects = List(MyStruct())
    sort(my_objects)
error: no matching function in call to 'sort'
note: candidate not viable: failed to infer parameter #0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.