You have more than one option here
1. As mentioned in some of the replies you can either loop through the array and create a new array, with elements inserted in the reverse order
2. Create a list from the array and reverse it using the Collections class
List list = Arrays.asList(your array);
Collections.reverse(list);
Now if you want to sort elements, create a comparator or Collator and use code similar to below.
Below is an example of using Comparator
import java.util.*;
Comparator stringComp = new Comparator(){
public int compare(Object o1, Object o2){
return ((
String)o1).compare( ((String)o2));
}
};
//Use the code below to
test it.
List wordsList = new ArrayList(){{
add("Andy");
add("Amanda");
add("Alabama");
add("Abey");
}};
Collections.sort(wordsList,stringComp);
for(Iterator itr =wordsList.iterator(); itr.hasNext(); ){
System.out.println(itr.next());
}
Now the above example works fine in most of the cases. String compareTo() and compare() method of the Comparator class uses the character encoding defined in Unicode encoding. equls() method looks for character equality. Fair enough right? Not always! Think globally ... In some languages, the sequence by the Unicode encoding doesn't match the dictionary order. So what is the alternative. Use java.text.Collator.
import java.text.*;
Collator c = Collator.getInstance(Locale.ITALY);
int result = c.compare("string1", "string2");
Now, if we combine the the power of both Comparator and Collator, we easily get a mechanism that can compare a collection of Strings that works independent of the language encoding.
import java.text.Collator;
import java.util.*;
final Collator collator = Collator.getInstance(); //default locale
Comparator advancedComp = new Comparator(){
public int compare(Object o1, Object o2){
return collator.compare( ((String)o1), ((String)o2));
}
};
//Test the code
Collections.sort(wordsList,advancedComp);
for(Iterator itr =wordsList.iterator(); itr.hasNext(); ){
System.out.println(itr.next());
//gives the same result in example above for default locale
}
Hope this helps
Sincerly
www.javaadvice.com team