Trait core::ops::Ord

pub trait Ord {
    /// Evaluates if one value of the same type is greater than another.
    ///
    /// # Arguments
    ///
    /// * `other`: [Self] - The value of the same type.
    ///
    /// # Returns
    ///
    /// * [bool] - `true` if `self` is greater than `other`, otherwise `false`.
    ///
    /// # Examples
    ///
    /// ```sway
    /// struct MyStruct {
    ///     val: u64,
    /// }
    ///
    /// impl Ord for MyStruct {
    ///     fn gt(self, other: Self) -> bool {
    ///         self.val > other.val
    ///     }
    /// }
    ///
    /// fn foo() {
    ///     let struct1 = MyStruct { val: 10 };
    ///     let struct2 = MyStruct { val: 2 };
    ///     let result = struct1 > struct2;
    ///     assert(result);
    /// }
    /// ```
    fn gt(self, other: Self) -> bool;
    /// Evaluates if one value of the same type is less than another.
    ///
    /// # Arguments
    ///
    /// * `other`: [Self] - The value of the same type.
    ///
    /// # Returns
    ///
    /// * [bool] - `true` if `self` is less than `other`, otherwise `false`.
    ///
    /// # Examples
    ///
    /// ```sway
    /// struct MyStruct {
    ///     val: u64,
    /// }
    ///
    /// impl Ord for MyStruct {
    ///     fn lt(self, other: Self) -> bool {
    ///         self.val < other.val
    ///     }
    /// }
    ///
    /// fn foo() {
    ///     let struct1 = MyStruct { val: 10 };
    ///     let struct2 = MyStruct { val: 2 };
    ///     let result = struct1 < struct2;
    ///     assert(!result);
    /// }
    /// ```
    fn lt(self, other: Self) -> bool;
}
Expand description

Trait to evaluate if one value is greater or less than another of the same type.

Required Methods

Evaluates if one value of the same type is greater than another.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is greater than other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Ord for MyStruct {
    fn gt(self, other: Self) -> bool {
        self.val > other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 > struct2;
    assert(result);
}

Evaluates if one value of the same type is less than another.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is less than other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Ord for MyStruct {
    fn lt(self, other: Self) -> bool {
        self.val < other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 < struct2;
    assert(!result);
}