package rebelsky.exam2; import java.util.Comparator; /** * Something that knows how to deal with things that can be ordered * (typically using a Comparator). * * @author Samuel A. Rebelsky * @version 1.0 of November 2005 */ public class Orderer { /** * Find the smallest value in an array, things, using c to * determine order. * * @return * smallest, a value in things * @pre * things is nonempty * @post * For all reasonable i, c.compare(smallest, things[i]) <= 0 * @post * Exists i, smallest == things[i] */ public T smallest(T[] things, Comparator c) { // Our first guess is the first thing in the array T smallest = things[0]; // Check all remaining items, refining our guess as necessary for (int i = 1; i < things.length; i++) { if (c.compare(things[i], smallest) < 0) { smallest = things[i]; } // if } // for // That's it, we're done return smallest; } // smallest(T[], Comparator) } // Orderer