Archive for March, 2014

Mar
06

a reverse Comparator works for all Objects implementing the Compareable interface

I should always sort the number arrays after the order of given columns. So I finally write a Comparator class which save all my breath. We should note the implementation for the Constructor, the arithmetic operators won’t work here. We have to use compareTo() method.

import java.util.Comparator;

/**
 * Sort arrays with comparable type reversely according the order of given
 * columns, the number of columns is not constained.
 * 
 * @author Ying
 * 
 */
public class ColumnComparator<T extends Number & Comparable<T>>
		implements Comparator<T[]> {

	int[] rang;
	int length;

	/**
	 * 
	 * @param cols
	 *            the Reverse Sort is after the column order, the numbers are
	 *            native number.
	 */
	public ColumnComparator(int... cols) {
		this.rang = cols;
		this.length = cols.length;
	}

	@Override
	public int compare(T[] a, T[] b) {

		for (int i = 0; i < length; i++) {

			int value = -a[rang[i] - 1].compareTo(b[rang[i] - 1]);

			if (value != 0) {
				return value;
			}

		}

		return 0;

	}

}

Example for usage:

	public static void main(String[] args) {

		Integer[][] arr = new Integer[][] { { 1, 2, 3 }, { 10, 12, 12 },
				{ 7, 8, 9 }, { 10, 11, 12 } };

		Arrays.sort(arr, new ColumnComparator<Integer>(1, 2));

	}