/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.calebfontenot.mason;

/**
 *
 * @author caleb
 */
public class Mason_2 {

    public static int maxNumberOfColumnsInJagged2dArray(char[][] ar) {
        int maxNumberOfColumns = 0;
        for (int row = 0; row < ar.length; ++row) {
            if (ar[row].length > maxNumberOfColumns) {
                maxNumberOfColumns = ar[row].length;
            }
        }

        return maxNumberOfColumns;
    }

    public static void printColumnMajorOrder(char[][] ar) {
        int row = 0;
        int column = 0;
        int max = maxNumberOfColumnsInJagged2dArray(ar);
        for (column = 0; column < max; ++column) {

            for (row = 0; row < ar.length; ++row) {
                if (column < ar[row].length) {
                    System.out.print(ar[row][column] + " ");
                }
            }
            System.out.println("");
        }
    }

    public static void sortNames(char[][][] names) {
        for (int numTable = 0; numTable < names.length; ++numTable) {
            for (int i = 0; i < names[numTable].length - 1; ++i) {

                for (int j = i + 1; j < names[numTable].length; ++j) {
                    //>compare character by character names[i] to names[j]
                    for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
                            //index2 to traverse names[j]
                            index1 < names[numTable][i].length
                            && index2 < names[numTable][j].length;
                            ++index1, ++index2) {

                        //>>SWAP name[i] with names[j]
                        //if names[i] > names[j] OR the name at i has more chars after the tie
                        // ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
                        if (names[numTable][i][index1] > names[numTable][j][index2]
                                || (index2 == names[numTable][j].length - 1 && index1 < names[numTable][i].length - 1)) {
                            //>> copy to tempI the names[i]
                            char[] temp = names[numTable][i];
                            names[numTable][i] = names[numTable][j];
                            names[numTable][j] = temp;
                            break;///you must break , no need to continue comparing characters at index1, index2
                        }
                        //>>if the characters are equal, continue
                        if (names[numTable][i][index1] == names[numTable][j][index2]) {
                            continue;//do nothing, but continue 
                        } else //name at i is smaller than name 
                        {
                            break;
                        }
                    }
                }
            }
        }
    }

    /**
     * Prints row by row
     *
     * @param ar
     */
    public static void printRowMajorOrder(char[][][] array) {
        for (char[][] i : array) {
            System.out.println("---------------------------------");
            printRowMajorOrder(i);
        }
    }

    public static void printRowMajorOrder(char[][] ar) {
        for (int i = 0; i < ar.length; ++i) {
            for (int j = 0; j < ar[i].length; ++j) {
                System.out.print(ar[i][j]);
            }
            System.out.print(" ");
            
        }
        System.out.println();
    }

    public static void main(String[] args) {
        char[][] names
                = {
                    {'j', 'o', 'h', 'n'}, {'a', 'n'}, {'b', 'y', 'r', 'o', 'n'},
                    {'b', 'y', 'r', 'o', 'n', 'i'}, {'a', 'a', 'o', 'n'},
                    {'b', 'b', 'b', 'b'}, {'b', 'b', 'b', 'c'}, {'b', 'b', 'b'}
                };
        char[][][] tables
                = {
                    {
                        {
                            'j', 'o', 'h', 'n'
                        },
                        {
                            'a', 'n'
                        },
                        {
                            'b', 'y', 'r', 'o', 'n'
                        },
                        {
                            'b', 'y', 'r', 'o', 'n', 'i'
                        }
                    },
                    {
                        {
                            'a', 'a', 'o', 'n'
                        },
                        {
                            'b', 'b', 'b', 'b'
                        },
                        {
                            'b', 'b', 'b', 'c'
                        },
                        {
                            'b', 'b', 'b'
                        }
                    }
                };
        System.out.println("ORIGINAL LIST");
        printRowMajorOrder(tables);

        System.out.println("\n\nSORTED LIST");
        sortNames(tables);
        printRowMajorOrder(tables);
    }
}
