/home/caleb/ASDV-Java/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java

    /*
     * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
     */
    package com.calebfontenot.lab.d.arrays.sort_calebfontenot;

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

        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("");
            }
        }

        /**
         * Prints row by row
         *
         * @param ar
         */
        public static void printRowMajorOrder(char[][] ar)//normal
        {
            for (int x = 0; x < ar.length; ++x) {
                for (int y = 0; y < ar[x].length; ++y) {
                    System.out.print(ar[x][y] + "");
                }
                System.out.println();
            }

        }
        public static String returnRowMajorOrder(char[] ar)//normal
        {
            String returnString = "";
            for (int x = 0; x < ar.length; ++x) {
                    returnString += ar[x];
                }
            return returnString;
        }

        /**
         * Sorts the methods in ascending order using Selection Sort
         *
         * @param names the names to be sorted
         */
        public static void sortNames(char[][] names)
        {
            for (int i = 0; i < names.length - 1; ++i) {
                for (int j = i + 1; j < names.length; ++j) {
                    char compChar1 = names[i][0], compChar2 = names[j][0];
                    // Reoder entire row
                    for (int rowIterate = 1; rowIterate < maxNumberOfColumnsInJagged2dArray(names); ++rowIterate) {
                        if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
                            try {
                                compChar1 = names[i][rowIterate];
                                compChar2 = names[j][rowIterate];
                            } catch (Exception ex) {
                                // If it's failed, the index has gone out of range.
                                // Check the length of the arrays and swap the larger one with the smaller one.
                                if (names[i].length > names[j].length) {
                                    //System.out.println(names[i].length + " " + names[j].length);
                                    //System.out.println("Swapping " + returnRowMajorOrder(names[i]) + " with " + returnRowMajorOrder(names[j]));
                                    char[] temp = names[i];
                                    names[i] = names[j];
                                    names[j] = temp;
                                }
                                break;
                            }

                        }
                        if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
                            char[] temp = names[i];
                            names[i] = names[j];
                            names[j] = temp;
                        }
                    }
                }
            }
        }

        public static void main(String[] args)
        {
            /*
    char[][] names = {
                {'j', 'o', 'h', 'n'},
                {'a', 'n'},
                {'b', 'y', 'r', 'o', 'n'},};
             */
            ///*
            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'}
            };
    //*/
            //printColumnMajorOrder(names);
            printRowMajorOrder(names);
            System.out.println();
            sortNames(names);
            //System.out.println();
            printRowMajorOrder(names);
            //printColumnMajorOrder(names);

        }

    }