import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Generates a list of random integers and outputs the contents in unsorted order, sorted 
 * numerically, and sorted by magnitude.
 * 
 * @author Drue Coles
 */
public class ComparatorDemo {

    public static void main(String[] args) {
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        final int minValue = -99;
        final int maxValue = 99;

        final int size = 10;
        Integer[] a = new Integer[size];
        
        System.out.print("Unsorted: ");
        for (int i = 0; i < size; i++) {
            a[i] = minValue + rand.nextInt(maxValue - minValue + 1);
            System.out.print(a[i] + " ");
        }
        System.out.println();
        
        System.out.print("Sorted numerically: ");
        Arrays.sort(a);
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();
        
        // for comparing integers by magnitude
        class MyComparator implements Comparator<Integer> {
            @Override
            public int compare(Integer t, Integer t1) {
                return Integer.compare(Math.abs(t), Math.abs(t1));
            }            
        }
        
        System.out.print("Sorted by magnitude: ");
        Arrays.sort(a, new MyComparator());
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}