//Coded by Gabriel Riogelon
import java.util.*;
import javax.swing.*;
public class NameSort {
    public static void main(String[] args){
        try{
            String[] names = new String[1048576];
            String input;
            String track = "";
            int x = 0;
            String temp = "";
            while (true) {
                input = JOptionPane.showInputDialog("Please input the names.\n(\"\\\" to delete last data, blank to start sorting)\n"+track);
                if(input.isEmpty()){
                    if(x>0){
                        x--;
                    }
                    break;
                }else if(input.equals("\\")){
                    if(x>0){
                        x--;
                        names[x] = "";
                        track = "";
                        for(int i = 0; i < x;i++){
                            track += names[i] + ", ";
                        }
                        track += "_";
                    }
                }else{
                    
                    names[x] = input;
                    track = "";
                        for(int i = 0; i <= x;i++){
                            track += names[i] + ", ";
                        }
                        track += "_";
                    x++;
                    
                }
            }
            String sort[] = new String[x+1];
            for(int i = 0; i <= x; i++){
                sort[i] = names[i];
            }
            for(int i = 0; i < sort.length-1; i++){
                for(int j = i+1; j < sort.length;j++){
                    for(int k = 0; k < sort[i].length() && k < sort[j].length();k++){
                        if(sort[i].toUpperCase().charAt(k)>sort[j].toUpperCase().charAt(k)){
                            temp = sort[i];
                            sort[i] = sort[j];
                            sort[j] = temp;
                            break;
                        }else if(sort[i].toUpperCase().charAt(k)<sort[j].toUpperCase().charAt(k)){
                            break;
                        }else if(k == sort[i].length()-1 || k == sort[j].length()-1){
                            if(sort[i].length()>sort[j].length()){
                            temp = sort[i];
                            sort[i] = sort[j];
                            sort[j] = temp;
                            break;
                            }
                        }
                    }
                }
            }
            String[] polish = new String[sort.length];
            for(int i = 0; i < sort.length;i++){
                polish[i] = "";
                polish[i] += sort[i].toUpperCase().charAt(0);
                for(int j = 1; j<sort[i].length();j++){
                    if(Character.isWhitespace(sort[i].charAt(j))&&Character.isWhitespace(sort[i].charAt(j+1))){
                        continue;
                    }else if (Character.isWhitespace(sort[i].charAt(j-1))){
                        polish[i] += sort[i].toUpperCase().charAt(j);
                    }else
                    polish[i] += sort[i].toLowerCase().charAt(j);
                }
            }
            JOptionPane.showMessageDialog(null, "Sorted names: " + Arrays.toString(polish));
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "An error has occured.\nYou have no input!");
        }
    }
}