I need to sort my array in alphabetical order. The program inputs N words(strings) and outputs them in alphabetical order. I have started out by using specific words to make sure everything is working correctly. I need to change this to accept any input. Any help would be appreciated!
public class Names
{
public static void main(String[ ] args)
{
String[ ] names = {"joe", "slim", "ed", "george"};
sortStringBubble (names);
for ( int k = 0; k < 4; k++ )
System.out.println( names [ k ] );
}
public static void sortStringBubble( String x [ ] )
{
int j;
boolean flag = true; // determine if sort is finished
String temp;
while ( flag )
{
flag = false;
for ( j = 0; j < x.length - 1; j++ )
{
if ( x [ j ].compareToIgnoreCase( x [ j+1 ] ) > 0 )
{ // ascending sort
temp = x [ j ];
x [ j ] = x [ j+1]; // swapping
x [ j+1] = temp;
flag = true;
}
}
}
}
}