Class NaturalSort

java.lang.Object
edu.cmu.tetrad.util.NaturalSort

public class NaturalSort extends Object
Provides functionality for natural sorting of strings and objects. Natural order refers to sorting where numeric components in strings are considered, for example, sorting "X1", "X2", and "X10" in that order rather than lexicographically as "X1", "X10", "X2".

Lagged names. A string of the form "name:lag" — exactly one colon, with the portion after the colon parseable as an integer — is treated as a lagged node name. Lagged names are grouped in order of decreasing lag (deepest lag first), and within a lag group are ordered by the natural order of the name before the colon. All strings that are not valid lagged names (no colon, more than one colon, or a non-integer suffix) come last, ordered naturally among themselves. This matches the layout convention for time-lagged data, where the deepest lag slice is displayed first and the contemporaneous (unlagged) variables are displayed last.

The comparators here are total on arbitrary strings and never throw: malformed inputs are simply treated as unlagged names.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Represents a natural key for a possibly-lagged node name of the form "name" or "name:lag".
    static final class 
    Represents a natural key, which consists of a string prefix and an optional numeric suffix.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Comparator<String>
    A comparator that sorts strings in natural order (e.g.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a comparator over possibly-lagged names that puts unlagged names first, then lagged names in order of increasing lag; within a lag group, and among the unlagged names, ordering is by the natural order of the base name, with the raw string as a final tiebreaker so that distinct names never compare equal.
    static <T> Comparator<T>
    Returns a comparator that sorts objects of any type in natural order, using each object's Object.toString() representation as the sort key.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NATURAL_NAME_COMPARATOR

      public static final Comparator<String> NATURAL_NAME_COMPARATOR
      A comparator that sorts strings in natural order (e.g. "X1", "X2", "X10" rather than "X1", "X10", "X2"). Valid lagged names ("name:lag") are grouped first, in order of decreasing lag, each group ordered by the natural order of the base name; all other strings come last, in natural order. Never throws on malformed input.
  • Method Details

    • naturalComparator

      public static <T> Comparator<T> naturalComparator()
      Returns a comparator that sorts objects of any type in natural order, using each object's Object.toString() representation as the sort key. Valid lagged names ("name:lag") group first in order of decreasing lag, then natural order of the base name; everything else comes last in natural order. Never throws on malformed input.
      Type Parameters:
      T - the type of objects to compare
      Returns:
      a natural-order comparator for T
    • lagAscendingComparator

      public static Comparator<String> lagAscendingComparator()
      Returns a comparator over possibly-lagged names that puts unlagged names first, then lagged names in order of increasing lag; within a lag group, and among the unlagged names, ordering is by the natural order of the base name, with the raw string as a final tiebreaker so that distinct names never compare equal. This is the display order used by GUI variable lists, where current-time variables belong at the top -- the reverse macro-order of naturalComparator(), which puts the deepest lags first for time-ordered listings. A name is treated as lagged under the same definition as NaturalSort.LaggedNaturalKey: exactly one colon with an integer suffix. Anything else, including names with non-integer colon suffixes such as "price:usd", sorts among the unlagged names, so comparison never throws. This replaces ad hoc GUI comparators that compared lag suffixes as strings, which put "X:10" before "X:2".
      Returns:
      a comparator imposing the display order described above