sorting - How to sort ArrayList alphabetically in Java without using collections? -


i have following code takes names , stores them in arraylist.

class main{   public static void main(string[] args) {     arraylist<string> names = new arraylist<string>();     scanner scan = new scanner(system.in);     string name;     do{       system.out.println("enter next name: ");       name = scan.nextline();       string touppercase = titlecase(name);       if(!touppercase.equals("stop")){         names.add(touppercase);       }     } while(!name.equalsignorecase("stop"));      system.out.println(names);     }   public static string titlecase(string s){     string output = s.substring(0, 1).touppercase() +     s.substring(1).tolowercase();     return output;   } 

}

i need sort arraylist alphabetically name without using collections.

string class implements comparable<string>. because of can use compareto method check if 2 strings in order. bubble sort 1 of inefficient easiest sorting algorithm out there.

so in pseudocode, like:

for int 0 size of list minus two:     if ("string @ index i".compareto("string @ index plus one") > 0)         swap 2 strings in list 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -