/******************************************************************************
 * Sorting a 2 dimensional array by any column (case insensitive)
 * Args: the column number to sort on (between 0 and 4)
 ******************************************************************************/
import java.util.*;

public class ArraySort2Dim
{
    public static void main (String[] args) 
    {   
        String[][] data = { {"Sam    ", "Sultan    ", "Male", "1965", "sam.sultan@nyu.edu"},
                            {"George ", "Washington", "Male", "1732", "GWashington@whitehouse.gov"},
                            {"John   ", "Adams     ", "Male", "1735", "John.Adams@whitehouse.gov"},
                            {"Thomas ", "Jefferson ", "Male", "1743", "TJ@whitehouse.gov"},
                            {"Abraham", "Lincoln   ", "Male", "1808", "honestAbe@whitehouse.gov"}
                          };

        int last_col = data[0].length - 1; 

        if (args.length != 1) 
        {
            System.out.println("Re-execute and pass a parameter from 0 to " + last_col);
            System.exit(-1);
        }

        int sortCol = Integer.parseInt(args[0]);        //get the arg column to sort on

        if (sortCol < 0 || sortCol > last_col) 
        {
            System.out.println("Re-execute and pass a parameter from 0 to " + last_col);
            System.exit(-1);
        }

        System.out.println("Before the sort");
        System.out.println("----------------------------------------------");

        for (int row=0; row < data.length; row++) 
        {
            for (int col=0; col < data[row].length; col++)
                System.out.print(data[row][col] + '\t');    //print before sort
        
            System.out.println();
        }
        
        System.out.println();

        Compare2Dim sortOrder  = new Compare2Dim(sortCol);  //create a comparator object
                                                            //... and pass it sort column requested
        Arrays.sort(data, sortOrder);                       //sort data array

        System.out.println("After the sort");
        System.out.println("----------------------------------------------");

        for (String[ ] line : data)
        {
            for (String element : line) 
                System.out.print(element + '\t');        //print after sort

            System.out.println();
        }
    }
}
/*************************************************************************************
 * Comparator classes for sorting
 * - This class sorts a 2 dimensional array by any column desired
 * - The constructor expects to recieve the column to sort on
 * - The compare method expect to receive two arrays of type Strings
 ************************************************************************************/  
class Compare2Dim implements Comparator<String[]>
{ 
    int sortCol;

    Compare2Dim(int col)                //constructor
    {
        sortCol = col;
    }
    
    public int compare(String[] a1, String[] a2) 
    {
        String s1, s2;                                  //declare 2 strings

        s1 = a1[sortCol];                               //obtain the element to sort on
        s2 = a2[sortCol];                
    
        if (sortCol == 3)
            return(Integer.parseInt(s1) - Integer.parseInt(s2));    //convert to int, and do numeric compare
        else
            return(s1.compareToIgnoreCase(s2));                     //alpha compare
                                                                    //positive means flip the elements
    }
}