This is the 1st HTML5 slant canvas

KIWY logo depicting a kiwi bird with a candle

k i w y

Need a light for your site?

This is the 2nd HTML5 slant canvas

Sorting Arrays

Natural Ordering

class Arrays

class java.util.Arrays is a utility class that contains various methods for manipulating arrays, including sorting and searching
// Sorting.java
// 
// This version uses the Comparator<T> interface (using generics)

import java.util.*;

// Example of how to sort an array several different ways
public class Sorting
{
    static String[] nameArray = {"Bob", "Fred", "Ralph", "Joe", "Wanda", 
                                 "Joanna", "billy Joe", "jennifer", "John"};

    public static void main(String[] args)
    {
        // print the original unsorted array
        System.out.println("");
        printRuleAndArray("Original name list:");

        // sorting by 'natural ordering'
        Arrays.sort(nameArray);
        printRuleAndArray("Sorted by 'natural ordering' " +
                                 "(lexicographic):");

        // sorting by length
        Arrays.sort(nameArray, new Comparator<String>()
            { 
                public int compare(String s1, String s2)
                {
                    return (s1.length() - s2.length());
                }
            });
        printRuleAndArray("Sorted list by name length:");

        // sorting alphabetically
        Arrays.sort(nameArray, new Comparator<String>()
            { 
                public int compare(String s1, String s2)
                {
                    return (s1.toLowerCase().compareTo(s2.toLowerCase()));
                }
            });
        printRuleAndArray("Sorted list in alphabetical order:");

        // sorting lexicographically by last letter 
        Arrays.sort(nameArray, new Comparator<String>()
            { 
                public int compare(String s1, String s2)
                {
                    return(s1.charAt(s1.length() - 1) 
			    - s2.charAt(s2.length() - 1) );
                }
            });
        printRuleAndArray("Sorted list in lexicographic order " +
                                 "of last letter:");
    }

    static void printRuleAndArray(String rule)
    {
        System.out.println(rule);
        for (int i = 0; i < nameArray.length; i++)
        {
            System.out.println(nameArray[i]);
        }
        System.out.println("");
    }

}      

Output from this example program

Original name list:
Bob
Fred
Ralph
Joe
Wanda
Joanna
billy Joe
jennifer
John

Sorted by 'natural ordering' (lexicographic):
Bob
Fred
Joanna
Joe
John
Ralph
Wanda
billy Joe
jennifer

Sorted list by name length:
Bob
Joe
Fred
John
Ralph
Wanda
Joanna
jennifer
billy Joe

Sorted list in alphabetical order:
billy Joe
Bob
Fred
jennifer
Joanna
Joe
John
Ralph
Wanda

Sorted list in lexicographic order of last letter:
Joanna
Wanda
Bob
Fred
billy Joe
Joe
Ralph
John
jennifer