import java.util.*;
public class Details {
public static void main(String args[]){
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.add("AA");
arraylist.add("ZZ");
arraylist.add("CC");
arraylist.add("FF");
System.out.println("Before Sorting:");
for(String str: arraylist){
System.out.println(str);
}
/* Sorting in decreasing order*/
}
}
Now how would I do this? Im new in this field. If this stupid then I would delete it but please help me I really need this to be answered.
As of Java 8, List has a sort method that takes a Comparator parameter.
The natural ordering for a list of strings is alphabetic order.
To sort the items of a list in their natural order you can use Comparator.naturalOrder() as the Comparator.
To sort the items of a list in the reverse of their natural order,
you can use Comparator.reverseOrder().
List<String> list = Arrays.asList("AA", "ZZ", "CC", "FF");
list.sort(Comparator.reverseOrder());
System.out.println(list);
If you are on an older version of Java,
though I don't see a good reason for that,
then you could use Collections.sort with Collections.reverseOrder() instead:
List<String> list = Arrays.asList("AA", "ZZ", "CC", "FF");
Collections.sort(list, Collections.reverseOrder());
System.out.println(list);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With