import java.util.Arrays; /* IntSorterAbsVal.java ** An instance of this class has methods (inherited from its parent) ** by which to sort an array (or a segment thereof) into ascending order ** using the classic Selection Sort algorithm. What this class provides ** is the isLessThan() method, whose results are consistent with ordering ** integers first based upon absolute value and then, to break ties, on ** value. That is, isLessThan(k,m) iff |k| < |m| or (|k| = |m| and k < m) */ public class IntSorterAbsVal extends Sorter { /* Returns true iff k < m according to the definition given above. */ protected boolean isLessThan(Integer k, Integer m) { int kAbs = Math.abs(k); int mAbs = Math.abs(m); return (kAbs < mAbs) || (kAbs == mAbs && k < m); } // main method simply for testing purposes public static void main(String[] args) { Integer[] ary = new Integer[args.length]; for (int i = 0; i != ary.length; i++) { ary[i] = Integer.parseInt(args[i]); } System.out.println("Array to be sorted:"); System.out.println(Arrays.toString(ary)); IntSorterAbsVal sorter = new IntSorterAbsVal(); sorter.sort(ary); System.out.println("Array after sorting:"); System.out.println(Arrays.toString(ary)); } }