
I've got another program that I need help with. I am trying to write a Java method that accepts an array of strings, and sorts the strings using the insertion sort algorithm. Then I need to write a main method to create a string array with 5 city names (e.g. Atlanta, New York, Dallas, Omaha, San Francisco), and display the results in alphabetical order. I've some small experience in sorting int arrays, but I'm drawing a blank with regards to String arrays.

Here is a code that sorts string array with Insertion Sort.
public class SortStringArrayUsingInsertionSort{
public static void main(String[] args){
String[] arr ={"Atlanta","New York","Dallas","Omaha","San Francisco"};
int count = 0;
String sortedArray[] = sort_sub(arr, arr.length);
for(int i=0;i<sortedArray.length;i++){
System.out.println(sortedArray[i]);
}
}
public static String[] sort_sub(String array[], int f){
String temp="";
for(int i=0;i<f;i++){
for(int j=i+1;j<f;j++){
if(array[i].compareToIgnoreCase(array[j])>0){
temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.