The method Collections.reverseOrder() after calling the Collections.sort() in order to reverse the list. It can be given in two ways:
1- We sort the list with the sort() method, then we use the reverse():
Collections.sort();and the second, the call to the reveseOrder() is done in the sort():
Collections.reverse();
Collections.sort(arraylist, Collections.reverseOrder());Example:
import java.util.ArrayList;Runtime:
import java.util.Collections;
public class sort_reverseorder_arraylist {
public static void main(String[] args) {
ArrayList< String> arraylist = new ArrayList< String> ();
//Add Items to ArrayList
arraylist.add("ac");
arraylist.add("ab");
arraylist.add("bb");
arraylist.add("aa");
arraylist.add("ae");
arraylist.add("ba");
System.out.println("ArrayList before sorting: "+arraylist);
Collections.sort(arraylist);
System.out.println("ArrayList after sorting: "+arraylist);
//reverse elements of ArrayList
Collections.reverse(arraylist);
System.out.println("ArrayList after inversion: "+arraylist);
}
}
ArrayList before sorting: [ac, ab, bb, aa, ae, ba]
ArrayList after sorting: [aa, ab, ac, ae, ba, bb]
ArrayList after inversion: [bb, ba, ae, ac, ab, aa]
Note: In this example we used a String type (ArrayList |
Commentaires (12)
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Excellent tutoriel !
N'hésitez pas si vous avez des questions.