By default, 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.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 sorted 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]
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Thanks, nice tip