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

comparators
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Comparators

Ryan Reid, Megan Lusk, Brian McClintock

slide-2
SLIDE 2

Sorting

  • Arrays.sort(anArray)
  • What if you don’t

want the default sort?

slide-3
SLIDE 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.

slide-4
SLIDE 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.

slide-5
SLIDE 5

How would you sort a group of vehicles?

slide-6
SLIDE 6

Benefits

  • No longer have to store values
  • Utilizes polymorphism
  • Easier to maintain
  • No need for a compareTo method
slide-7
SLIDE 7

Demo

slide-8
SLIDE 8