Java Program to sort a List in case sensitive order

Java 8Object Oriented ProgrammingProgramming

Let’s say your list is having the following elements −

P, W, g, K, H, t, E

Therefore, case sensitive order means, at first the sorted order would be in capital letters and then small −

E, H, K, P, W, g, t

The following is our array −

String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };

Convert the above array to a List −

List<String>list = Arrays.asList(arr);

Now sort the above list in case sensitive order −

Collections.sort(list);

Example

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
   public static void main(String[] arg v) throwsException {
      String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };
      List<String>list = Arrays.asList(arr);
      System.out.println("List = "+list);
      Collections.sort(list);
      System.out.println("Case Sensitive Sort = "+list);
   }
}

Output

List = [P, W, g, K, H, t, E]
Case Sensitive Sort = [E, H, K, P, W, g, t]
raja
Published on 24-Apr-2019 11:29:48
Advertisements