Java Comparator create case insensitive Comparator

Description

Java Comparator create case insensitive Comparator

import java.util.Arrays;
import java.util.Comparator;

public class Main {

  public static void main(String[] args) {
    String[] sa = new String[] {"A","C","D", "a", "c", "d" };
    AlphabeticComparator comp = new AlphabeticComparator();
    Arrays.sort(sa, comp);//from w w  w .  j a  va2 s .c o  m
    
    System.out.println(Arrays.toString(sa));
  }
}

class AlphabeticComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    String s1 = (String) o1;
    String s2 = (String) o2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
}



PreviousNext

Related