Sort string array in case insensitive order and case sensitive order java

Let’s learn to sort string array in case insensitive order and case sensitive order java.

Sort string array in case insensitive order and case sensitive order java

How do I sort array values in case insensitive order is the most common java interview question.

sort string array in case insensitive order and case sensitive order java

Here’s the java program to sort an array in case-insensitive order and sensitive order,

Also read – access modifiers in java

import java.util.Arrays;
public class SortStringArrayDemo
{
   public static void main(String[] args) 
   {
      String[] fruits = new String[5];
      fruits[0] = "Mango";
      fruits[1] = "cherries";
      fruits[2] = "Apple";
      fruits[3] = "lychee ";
      fruits[4] = "Elderberry";
      Arrays.sort(fruits);
      System.out.println("CASE SENSITIVE ORDER : ");
      System.out.println(Arrays.toString(fruits));
      Arrays.sort(fruits, String.CASE_INSENSITIVE_ORDER);
      System.out.println("CASE INSENSITIVE ORDER : ");
      System.out.println(Arrays.toString(fruits));
   }
}



Output:

CASE SENSITIVE ORDER :
[Apple, Elderberry, Mango, cherries, lychee]
CASE INSENSITIVE ORDER :
[Apple, cherries, Elderberry, lychee , Mango]


case insensitive order java

Let’s learn case insensitive order java using List. In case insensitive order java,

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CaseInsensitiveOrder 
{
   public static void main(String[] args) 
   {
      String[] arrAnimals = new String[] { "Panda", "Wolf", "giraffe", "Kangaroo", "Hawk", "tiger" };
      List<String> li = Arrays.asList(arrAnimals);
      System.out.println("Given list = " + li);
      Collections.sort(li);
      System.out.println("Case sensitive order sort = " + li);
      Collections.sort(li, String.CASE_INSENSITIVE_ORDER);
      System.out.println("Case insensitive order java = " + li);
   }
}


Output:

Given list = [Panda, Wolf, giraffe, Kangaroo, Hawk, tiger]
Case sensitive order sort = [Hawk, Kangaroo, Panda, Wolf, giraffe, tiger]
Case insensitive order java = [giraffe, Hawk, Kangaroo, Panda, tiger, Wolf]


comparator.comparing case insensitive

Here we are going to learn comparator.comparing case insensitive java 8.

CASE_INSENSITIVE_ORDER
public static final Comparator<String> CASE_INSENSITIVE_ORDER

A Comparator that orders String objects as by compareToIgnoreCase. This comparator is serializable.

NOTE:

This Comparator does not take locale into account, and will result in an unsatisfactory ordering for certain locales.

The java.text package provides Collators to allow locale-sensitive ordering.

Here’s the java program on comparator.comparing case insensitive,

public class Student
{
   private String studentName;
   private Integer studentAge;
   public Student(String name, Integer age)
   {
      super();
      studentName = name;
      studentAge = age;
   }
   public String getName()
   {
      return studentName;
   }
   public Integer getAge()
   {
      return studentAge;
   }
   @Override
   public String toString()
   {
      return "Student [Student name = " + studentName + ", age = " + studentAge + "]";
   }
}

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CaseInsensitiveExample 
{
   public static void main(String[] args) 
   {
      List<Student> obj = new ArrayList<>();
      obj.add(new Student("virat", 30));
      obj.add(new Student("Dhoni", 28));
      obj.add(new Student("rohit", 24));
      obj.add(new Student("Ajinkya", 23));
      obj.add(new Student("bhuvaneshwar", 27));
      obj.add(new Student("Ishant", 25));
      System.out.println("Before case insensitive: ");
      obj.forEach(System.out::println);
      System.out.println("------------------------------------------");
      Comparator<Student> com = Comparator.comparing(Student::getName, String.CASE_INSENSITIVE_ORDER);
      Collections.sort(obj, com);
      System.out.println("After case insensitive: ");
      obj.forEach(System.out::println);
   }
}


Output:

Before case insensitive:
Student [Student name = virat, age = 30]
Student [Student name = Dhoni, age = 28]
Student [Student name = rohit, age = 24]
Student [Student name = Ajinkya, age = 23]
Student [Student name = bhuvaneshwar, age = 27]

Student [Student name = Ishant, age = 25]

After case insensitive:
Student [Student name = Ajinkya, age = 23]
Student [Student name = bhuvaneshwar, age = 27]
Student [Student name = Dhoni, age = 28]
Student [Student name = Ishant, age = 25]
Student [Student name = rohit, age = 24]
Student [Student name = virat, age = 30]


case insensitive collection in java

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionCaseInsensitive 
{
   public static void main(String[] args) 
   {
      String[] strAnimals = new String[] { "zebra", "Alligator", "jackal", "Bear", "lion", "Tiger" };
      List li = Arrays.asList(strAnimals);
      Collections.sort(li);
      System.out.println("Before case insensitive: " + li);
      Collections.sort(li, String.CASE_INSENSITIVE_ORDER);
      Collections.sort(li, Collections.reverseOrder());
      Collections.sort(li, String.CASE_INSENSITIVE_ORDER);
      Collections.reverse(li);
      System.out.println("After case insensitive: " + li);
   }
}


Output:

Before case insensitive: [Alligator, Bear, Tiger, jackal, lion, zebra]
After case insensitive: [zebra, Tiger, lion, jackal, Bear, Alligator]