Yes, use a comparator. Or have the data type implement the Comparable interface.