By default the when sorting an arrays the value will be ordered in case-sensitive order. This example show you how to order it in case-insensitive order.
package org.kodejava.example.util;
import java.util.Arrays;
public class SortArrayCaseSensitivity {
public static void main(String[] args) {
String[] teams = new String[5];
teams[0] = "Manchester United";
teams[1] = "chelsea";
teams[2] = "Arsenal";
teams[3] = "liverpool";
teams[4] = "EVERTON";
// Sort array, by default it will be sort in case sensitive order.
// [Arsenal, EVERTON, Manchester United, chelsea, liverpool]
Arrays.sort(teams);
System.out.println("Case sensitive : " + Arrays.toString(teams));
// Sort array in case insensitive order
// [Arsenal, chelsea, EVERTON, liverpool, Manchester United]
Arrays.sort(teams, String.CASE_INSENSITIVE_ORDER);
System.out.println("Case insensitive: " + Arrays.toString(teams));
}
}
The result of the code snippet above:
Case sensitive : [Arsenal, EVERTON, Manchester United, chelsea, liverpool]
Case insensitive: [Arsenal, chelsea, EVERTON, liverpool, Manchester United]
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019
Thanks, nice tip