by: custom comparator or projection

Component for methods that take a custom projection or comparison function.

// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

impl<T> [T] {
    fn sort(&mut self) where T: Ord;

    fn sort_by(&mut self, compare: impl FnMut(&T, &T) -> Ordering);

    fn sort_by_key<K, F>(&mut self, f: F)
    where
        F: FnMut(&T) -> K,
        K: Ord;
}
  • sort_by takes a custom comparator function that replaces the normal Ord comparison logic.

  • sort_by_key takes a projection function that takes the original element and returns an alternate value to use for sorting. This allow us to do things like sort by a particular field of a struct.

  • Sometimes the “by” preposition is simply a preposition: