comparators
play

Comparators Ryan Reid, Megan Lusk, Brian McClintock Sorting - PowerPoint PPT Presentation

Comparators Ryan Reid, Megan Lusk, Brian McClintock Sorting Arrays.sort(anArray) What if you dont want the default sort? Comparators What is a comparator? An Object that implements the Comparator Interface. A Comparator


  1. Comparators Ryan Reid, Megan Lusk, Brian McClintock

  2. Sorting • Arrays.sort(anArray) • What if you don’t want the default sort?

  3. Comparators • What is a comparator? • An Object that implements the Comparator Interface. • A Comparator must use the compare function which takes more than one Object • public int compare(T object, T object) Provides a way to order things that have no natural order.

  4. Uses • Provides a way to order things that don’t have a natural order . • User must create a comparator class to determine how the array will be sorted.

  5. How would you sort a group of vehicles?

  6. Benefits • No longer have to store values • Utilizes polymorphism • Easier to maintain • No need for a compareTo method

  7. Demo

Recommend


More recommend