2
   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.

CSE B
  • 43
  • 5
  • 2
    Answer is here - https://stackoverflow.com/questions/16252269/how-to-sort-an-arraylist – vinS Dec 10 '17 at 07:09

4 Answers4

5

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);
janos
  • 120,954
  • 29
  • 226
  • 236
2

you can use Comparator before loop:

Collections.sort(arraylist , new Comparator<String>() {
   public int compare(String o1, String o2) {
     return o1.compareTo(o2); 
   }
});
Amir Azizkhani
  • 1,662
  • 17
  • 30
2
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*/
     Collections.sort(arraylist);
     Collections.reverse(arraylist);
     for(String str: arraylist){
          System.out.println(str);
      }

  }
}
Suvam Roy
  • 963
  • 1
  • 8
  • 19
1

You could do:

   import java.util.*;
public class TicTacToe  {

  public static void main(String args[]){
     ArrayList<String> arraylist = new ArrayList<String>();
     arraylist.add("AA");
     arraylist.add("ZZ");
     arraylist.add("CC");
     arraylist.add("FF");
     Collections.sort(arraylist);


     System.out.println("Before Sorting:");
     for(String str: arraylist){
          System.out.println(str);
      }
     Collections.sort(arraylist, Collections.reverseOrder());
   /* Sorted List in reverse order*/
   System.out.println("ArrayList in descending order:");
   for(String str: arraylist){
        System.out.println(str);
     }
  }
}

This will sort it alphabetically then reverse it.