/******************************************************************************
 * Sorting a 2 dimensional numerical ArrayList by any column 
 * Args: the column number to sort on (between 0 and 9)
 ******************************************************************************/
import java.util.*;

public class BArrayListSort2Dim
{
    public static void main (String[] args) 
    {   
        if (args.length != 1) 
        {
            System.out.println("Re-execute and pass 1 parameter from 0 to 9");
            System.exit(-1);
        }
    
        int sortCol = Integer.parseInt(args[0]);                            //get the arg column to sort on

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


        ArrayList <List<Double>> data = new ArrayList <List<Double>> ();    //define a 2dim ArrayList    

        for (int i=0; i<10; i++)
        {
            ArrayList<Double> row  = new ArrayList<Double>();               //define a 1dim ArrayList

            for (int j=0; j<10; j++)
            {
                double num;
                num = Math.random();                        //generate a random number
                num = Math.round(num*10000)/100.0;          //make it 99.99             
                row.add(num);                               //store num in 1dim ArrayList 
            }
            data.add(row);                                  //store row in 2dim ArrayList
        }

        System.out.println("Before the Sort -----------------------------------------");
        for (int i=0; i<10; i++)
            System.out.println(data.get(i));

        System.out.println();

        Compare2DimNum sortOrder  = new Compare2DimNum(sortCol);    //create a comparator object

        Collections.sort(data, sortOrder);                          //sort the 2 dim ArrayList

        System.out.println("After the Sort ------------------------------------------");
        for (int i=0; i<10; i++)
            System.out.println(data.get(i));
    }
}

/*************************************************************************************
 * Comparator classes for sorting
 * - This class sorts a 2 dimensional ArrayList by any column desired
 * - The constructor expects to recieve the column to sort on
 * - The compare method expect to receive two Lists of type Double
 ************************************************************************************/  
class Compare2DimNum implements Comparator <List<Double>>
{ 
    int sortCol;

    Compare2DimNum(int col)             //constructor
    {
        sortCol = col;
    }
    
    public int compare(List<Double> a1, List<Double> a2) 
    {
        Double num1, num2;                  //declare 2 Double variables

        num1 = a1.get(sortCol);              //obtain the element to sort on
        num2 = a2.get(sortCol);              
    
        int value = num1 > num2 ? 1 : -1;   //if f1 > f2 = 1 (flip)
        return (value); 
    }
}