// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.*;

public class TwoDimSort {
   public static void main(String[] args) {
      int[][] a = { { 1, 4, 3, 2 }, { 0, 0, 4, 2 }, { 1, 3, 3, 8 } };
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 4; j++) {
            System.out.print(" " + a[i][j]);
         }
         System.out.println();
      }
      Arrays.sort(a, new Cmp());
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 4; j++) {
            System.out.print(" " + a[i][j]);
         }
         System.out.println();
      }
   }
}

class Cmp implements Comparator {
   public int compare(Object o1, Object o2) {
      int[] a1 = (int[]) o1;
      int[] a2 = (int[]) o2;
      int k = 0;
      for (;;) {
         if (k == a1.length && k == a2.length) {
            return 0;
         } else if (k == a1.length && k < a2.length) {
            return -1;
         } else if (k < a1.length && k == a2.length) {
            return 1;
         } else if (a1[k] == a2[k]) {
            k++;
         } else if (a1[k] < a2[k]) {
            return -1;
         } else if (a1[k] > a2[k]) {
            return 1;
         }
      }
   }
   public boolean equals(Object obj) {
      return false;
   }
}
