/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */

package com.calebfontenot.mason;

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

    public static void sortNames(char[][] names)
    {

        for (int i = 0; i < names.length - 1; ++i)
        {

            for (int j = i + 1; j < names.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[i].length
                        && index2 < names[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[i][index1] > names[j][index2]
                            || (index2 == names[j].length - 1 && index1 < names[i].length - 1))
                    {
                        //>> copy to tempI the names[i]
                        char[] temp = names[i];
                        names[i] = names[j];
                        names[j] = temp;
                        break;///you must break , no need to continue comparing characters at index1, index2
                    }
                    //>>if the characters are equal, continue
                    if (names[i][index1] == names[j][index2])
                    {
                        continue;//do nothing, but continue 
                    } else //name at i is smaller than name 
                    {
                        break;
                    }
                }
            }
        }

    }

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

                for (int j = i + 1; j < tables[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 < tables[i][i].length
                            && index2 < tables[j][i].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 (tables[numTable][i][index1] > tables[numTable][j][index2]
                                || (index2 == tables[numTable][j].length - 1 && index1 < tables[numTable][i].length - 1))
                        {
                            //>> copy to tempI the names[i]
                            char[][] temp = tables[i];
                            tables[i] = tables[j];
                            tables[j] = temp;
                            break;///you must break , no need to continue comparing characters at index1, index2
                        }
                        //>>if the characters are equal, continue
                        if (tables[i][index1] == tables[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[][] 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(" ");

        }
    }

    public static void printRowMajorOrder(char[][][] tables)
    {

        for (int numTable = 0; numTable < tables.length; ++numTable)
        {
            for (int i = 0; i < tables[numTable].length; ++i)
            {
                for (int j = 0; j < tables[numTable][i].length; ++j)
                {
                    System.out.print(tables[numTable][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(names);
//
//        System.out.println("\n\nSORTED LIST");
//        sortNames(names);
//        printRowMajorOrder(names);

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

}
