/******************************************************************************
 * Sorting incoming strings on the command line using an Arraylist
 * Using lambda functions 
 ******************************************************************************/
import java.util.*;

public class BArrayListSort
{
    public static void main (String[] words) 
    {
        if (words.length < 2) 
        {
            System.out.println("Re-execute with at least 2 words");
            System.exit(-1);
        }
        
        ArrayList<String> list = new ArrayList<String>();

        for (int i=0; i < words.length; i++) 
            list.add(words[i]);                         //add to ArrayList

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

        System.out.println(list);                       //print entire ArrayList

        for (int i=0; i < list.size(); i++)             //print each element 
            System.out.println(list.get(i));

        System.out.println();

// ---- choose one of the following ------------------------------------------------------------------------------------

//      Collections.sort(list);                                                             //sort arrayList Asc

//      Collections.sort(list, Collections.reverseOrder());                                 //sort arrayList Descending
    
//      Collections.sort(list, String.CASE_INSENSITIVE_ORDER);                              //sort arrayList Asc, Not case sensitive

//      Collections.sort(list, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));    //sort arrayList Desc, Not case sensitive

//      Collections.sort(list, (a, b) -> (Integer.parseInt(a) - Integer.parseInt(b)) );             //sort Asc, integer only
//      Collections.sort(list, (a, b) -> (int) (Double.parseDouble(a) - Double.parseDouble(b)));    //sort Asc, any number 
//      Collections.sort(list, (a, b) -> (Integer.parseInt(b) - Integer.parseInt(a)) );             //sort Desc, integer only
//      Collections.sort(list, (a, b) -> (int) (Double.parseDouble(b) - Double.parseDouble(a)));    //sort Desc, any number 

//      Collections.sort(list, (a, b) -> a.length() - b.length());                                  //sort by length of word

                                                                                                    //sort by last 3 characters
/*                                                                                                  //multi-line lambda function
        Collections.sort(list, (a, b) -> { int start1 = Math.max(0, a.length()-3 );     //0 or length-3
                                           int start2 = Math.max(0, b.length()-3 );     //0 or length-3	
                                           a = a.substring(start1);                     //take the last 3 characters
                                           b = b.substring(start2);                     //take the last 3 characters
                                           return(a.compareTo(b)); }                    //if a > b  --> value = positive
                    );
*/                    
        Collections.sort(list, (a, b) -> a.substring(Math.max(0,a.length()-3)).compareTo(b.substring(Math.max(0,b.length()-3))));  //the same, all on 1 line                                  //sort by length of word

// ---------------------------------------------------------------------------------------------------------------------

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

        System.out.println(list);                       //print entire ArrayList

        for (int i=0; i < list.size(); i++)             //print each element 
            System.out.println(list.get(i));

        System.out.println();
        
    }
}