// 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.Random;

public class Sort2D {
    private final static int N = 10; // rows
    private final static int M = 3; // columns
    private final static int KEY = 0; // second dimension that we sort after 0=first column
    private static Random rng = new Random();
    private static void init(int[][] a) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < M; j++) {
                a[i][j] = rng.nextInt(1000);
            }
        }
    }
    private static void print(int[][] a) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < M; j++) {
                System.out.print(" " + a[i][j]);
            }
            System.out.println();
        }
    }
    private static void sort(int[][] a) {
        for(int i = 0; i < N; i++) {
            for(int i2 = (i + 1); i2 < N; i2++) {
                if(a[i][KEY] > a[i2][KEY]) {
                    int tmp;
                    for(int j = 0; j < M; j++) {
                        tmp = a[i][j];
                        a[i][j] = a[i2][j];
                        a[i2][j] = tmp;
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        int[][] a = new int[N][M];
        init(a);
        System.out.println("Unsorted:");
        print(a);
        System.out.println("Sorted:");
        sort(a);
        print(a);
    }
}
