English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Suppose there is a String array with 5 names already.....How do I sort this array in alphabetical order??
(I need the program not the logic)

2007-03-14 23:37:50 · 3 answers · asked by kri91-16 2 in Computers & Internet Programming & Design

Sorting using the compareTo() function
(I use JDK 3)

2007-03-15 05:42:23 · update #1

3 answers

Do your own homework..... B.... Too bad the person up there already answered ur question.....

2007-03-15 00:16:12 · answer #1 · answered by Geinius 3 · 0 0

There are several ways.Good things to use some collections classes from java.uitl package.
Following is the code to sort a string array which uses collections.

String[] strArray = new String[] {"z", "a", "C"};
Arrays.sort(strArray);
// [C, a, z]


// Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
// [a, C, z]

// Reverse-order sort
Arrays.sort(strArray, Collections.reverseOrder());
// [z, a, C]

// Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
// [z, C, a]


This works fine.

2007-03-14 23:51:41 · answer #2 · answered by Siva 3 · 0 0

String array[] = { "May", "Lara", "Jason", "John", "Adam", "Angel"};

// Here we use a Linear Sort - the simplest sorting algorithm
int x;
String temp;
for(int i=0; i { x = i;
for(int j=i; j { if(array[j].compareTo( array[x] ) < 0)
{ x = j; }
}
temp = array[x];
array[x = array[i];
array[i] = temp;
}

2007-03-18 21:16:36 · answer #3 · answered by Liviawarty J 2 · 0 0

fedest.com, questions and answers