Java Program to sort a List in case sensitive order


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]

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

522 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements