Java Program to Sort Strings in an Alphabetical Order

preview_player
Показать описание
In this program, we are asking user to enter the count of strings that he would like to enter for sorting. Once the count is captured using Scanner class, we have initialized a String array of the input count size and then are running a for loop to capture all the strings input by user.

Once we have all the strings stored in the string array, we are comparing the first alphabet of each string to get them sorted in the alphabetical order.

public class JavaExample
{
public static void main(String[] args)
{
int count;
String temp;

//User will be asked to enter the count of strings


String str[] = new String[count];

//User is entering the strings and they are stored in an array
for(int i = 0; i _ count; i++)
{
}

//Sorting the strings
for (int i = 0; i _ count; i++)
{
for (int j = i + 1; j _ count; j++) {
if (str[i].compareTo(str[j])_0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

//Displaying the strings after sorting them based on alphabetical order
for (int i = 0; i _= count - 1; i++)
{
}
}
}
Рекомендации по теме