I wrote this program for sorting an array of type String
alphabetical without using compareTo()
method so I am saying that if you can find errors in my program please tell me I will try to fix it.
class some{
public static void main(String[] args) {
String array[]={"names here"};
System.out.print("before:");
for (int i=0 ;i<array.length;i++ )
System.out.print(array[i]+" ");
array = SortA(array);
System.out.println();
System.out.print("after:");
for (int i=0 ;i<array.length;i++ )
System.out.print(array[i]+" ");
}//end of main method
public static int getSmallestRange(String[] array){
int range = array[0].length()-1;
for (int i =0;i<array.length ;i++ ) {
if(range>array[i].length()-1){
range = array[i].length()-1;
}
}
return range ;
}
public static String[] SortA(String [] array){
String tmp="";
int index = 0 ;
for (int i =0;i<array.length ;i++ )
for (int j = i+1;j<array.length ;j++ ) {
if(array[i].charAt(index)>array[j].charAt(index)){
tmp = array[i];
array[i] = array[j];
array[j] = tmp ;
}//end of if
}//end of loop
index++;
for (int x =0;x<array.length-1 ;x++ ){
for (int y =x+1;y<array.length ;y++ ) {
if(array[x].charAt(0)==array[y].charAt(0)){
if(array[x].charAt(index)>array[y].charAt(index)){
tmp = array[x];
array[x] = array[y];
array[y] = tmp ;
}
else if(array[x].charAt(index)==array[y].charAt(index)){
if(index<getSmallestRange(array))
index++;
}
}//end of if
}//end of loop
}
return array;
}//end of method
}
{"dds", "dda"}
, it fails to sort them properly. – bowmore Jun 2 '13 at 6:50