Source of TestIntegerArraySorting.java


  1: //TestIntegerArraySorting.java

  3: import java.util.Arrays;
  4: import java.util.Scanner;

  6: public class TestIntegerArraySorting
  7: {
  8:     public static void main(String[] args)
  9:     {
 10:         Scanner keyboard = new Scanner(System.in);

 12:         int[] intArray = { 32, 3, 145, 78, 44, 61, 27 };

 14:         System.out.println("\nPrinting values in the order entered ... ");
 15:         for (int i : intArray)
 16:         {
 17:             System.out.print(i + " ");
 18:         }
 19:         System.out.print("\nPress Enter to continue ... ");
 20:         keyboard.nextLine();

 22:         System.out.println
 23:         (
 24:             "\nSorted in the default way "
 25:             + "(increasing order) ... "
 26:         );
 27:         Arrays.sort(intArray);
 28:         for (int i : intArray)
 29:         {
 30:             System.out.print(i + " ");
 31:         }
 32:         System.out.print("\nPress Enter to continue ... ");
 33:         keyboard.nextLine();

 35:         System.out.println("\nSorted in increasing order by last digit ... ");
 36:         Integer[] integerArray =
 37:         {
 38:             32, 3, 145, 78, 44, 61, 27
 39:         };
 40:         Arrays.sort
 41:         (
 42:             integerArray,
 43:             (Integer a, Integer b) ->
 44:             Integer.compare(new Integer(a % 10), new Integer(b % 10))
 45:         );
 46:         for (int i : integerArray)
 47:         {
 48:             System.out.print(i + " ");
 49:         }
 50:         System.out.print("\nPress Enter to continue ... ");
 51:         keyboard.nextLine();

 53:         System.out.println("\nSorted in decreasing order by digit sum ... ");
 54:         Integer[] integerArray2 = { 32, 3, 145, 78, 44, 61, 27 };
 55:         Arrays.sort(integerArray2, (Integer a, Integer b) ->
 56:         {
 57:             int a_sum = 0;
 58:             while (a / 10 != 0)
 59:             {
 60:                 a_sum += a % 10;
 61:                 a /= 10;
 62:             }
 63:             a_sum += a;
 64:             int b_sum = 0;
 65:             while (b / 10 != 0)
 66:             {
 67:                 b_sum += b % 10;
 68:                 b /= 10;
 69:             }
 70:             b_sum += b;
 71:             return b_sum - a_sum;
 72:             //The above line can/should be replaced by the
 73:             //following recommended "better" alternative:
 74:             //return Integer.compare(b_sum, a_sum);
 75:         });
 76:         for (int i : integerArray2)
 77:         {
 78:             System.out.print(i + " ");
 79:         }
 80:         System.out.print("\nPress Enter to continue ... ");
 81:         keyboard.nextLine();
 82:     }
 83: }
 84: /*  Output:

 86:     Printing values in the order entered ...
 87:     32 3 145 78 44 61 27
 88:     Press Enter to continue ...

 90:     Sorted in the default way (increasing order) ...
 91:     3 27 32 44 61 78 145
 92:     Press Enter to continue ...

 94:     Sorted in increasing order by last digit ...
 95:     61 32 3 44 145 27 78
 96:     Press Enter to continue ...

 98:     Sorted in decreasing order by digit sum ...
 99:     78 145 27 44 61 32 3
100:     Press Enter to continue ...
101: */