AlphabeticSorting.java Source code

Java tutorial

Introduction

Here is the source code for AlphabeticSorting.java

Source

// : c11:AlphabeticSorting.java
//Keep upper and lowercase letters together.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.

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

public class AlphabeticSorting {
    public static void main(String[] args) {
        String[] sa = new String[] { "a", "c", "b" };

        System.out.println("Before sorting: " + Arrays.asList(sa));
        Arrays.sort(sa, new AlphabeticComparator());
        System.out.println("After sorting: " + Arrays.asList(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());
    }
} ///:~