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

public class ArraySort
{
    public static void main (String[] words) 
    {
        if (words.length < 2) 
        {
            System.out.println("Re-execute with at least 2 words");
            System.exit(-1);
        }
    
        System.out.println("Before the sort");
        System.out.println("---------------");

        for (int i=0; i < words.length; i++)        //print each word 
            System.out.println(words[i]);

        System.out.println();

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

//      Arrays.sort(words);                                                                     //sort array Asc

//      Arrays.sort(words, Collections.reverseOrder());                                         //sort array Descending

//      Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);                                      //sort array Asc, Not case sensitive

//      Arrays.sort(words, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));            //sort array Desc, Not case sensitive

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

//      Arrays.sort(words, (a, b) -> a.length() - b.length());                                  //sort by length of word

                                                                                                //sort by last 3 characters
                                                                                                //multi-line lambda function
        Arrays.sort(words, (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
                    );
                    
//      Arrays.sort(words, (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("--------------");

        for (int i=0; i < words.length; i++)  
            System.out.println(words[i]);       //print after sort
    }
}