0

I am working on a problem. I am almost done but the very last of my code is confusing me. I knew that Collections.sort() sorts word alphabetical order. But this code is not. In the java api it says: Sorts the specified list in to ascending order. Please correct me or provide some info.Here is the code:

import java.util.*;
class Ideone
{
   public static void main (String[] args)
   {
     List <String> list = new ArrayList();
     list.add("derail");list.add("Disk");list.add("advocate");list.add("ladder");
     list.add("soon");list.add("eye");list.add("NotE");list.add("Bobycott");
     Collections.sort(list);
     for (String ananagram : list) {
        System.out.print(ananagram+" ");
     }
   }
}

output is :

Bobycott Disk NotE advocate derail eye ladder soon

But I was Expecting:

advocate Bobycott eye derail Disk ladder NotE soon
mlhazan
  • 29
  • 5

1 Answers1

0

You need to use something like this:

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

All that happens, cause in general char 'A' isn't equal to 'a' (they have different codes)

Mysterion
  • 9,050
  • 3
  • 30
  • 52