Trait ColumnOrdering

Source
pub trait ColumnOrdering<Row>
where Row: Clone + Send + Sync,
{ // Required method fn order_by(&self, row_1: &Row, row_2: &Row) -> Ordering; }
Expand description

Trait for defining how to order rows based on a specific column.

This trait should be implemented by users to specify how rows should be compared for sorting purposes. The implementation can vary depending on the type of column. For instance, string comparisons or numeric comparisons can be handled differently depending on the column. Should only be implemented for Ascending ordering, in case of Descending, it is handled internally.

§Example

Suppose you have a struct MyRow with fields like user_id, name, and username. You could implement this trait for each column to specify how rows should be compared.

impl ColumnOrdering<MyRow> for ColumnName {
    fn order_by(&self, row_1: &MyRow, row_2: &MyRow) -> Ordering {
        match self {
            ColumnName::UserID => row_1.user_id.cmp(&row_2.user_id),
            ColumnName::Name => row_1.name.cmp(&row_2.name),
            ColumnName::Username => row_1.username.cmp(&row_2.username),
        }
    }
}

Required Methods§

Source

fn order_by(&self, row_1: &Row, row_2: &Row) -> Ordering

Compare two rows and return the ordering result (Ordering).

This function defines how to order two rows based on the specific column. It returns Ordering::Less, Ordering::Equal, or Ordering::Greater to indicate whether row_1 should be placed before, after, or at the same position as row_2 when sorting. Should only be implemented for ascending ordering, in case of Descending, it is handled internally.

§Arguments
  • row_1 - The first row for comparison.
  • row_2 - The second row for comparison.
§Returns
  • Ordering - Indicates the relative order between the two rows.

Implementors§