Return to Snippet

Revision: 52388
at October 21, 2011 08:18 by binaryadder


Initial Code
import java.util.*;

   public class SortName{
      public static void main(String[] argsv){
        Scanner s = new Scanner(System.in);
        System.out.print("How many data items do you wish to enter: ");
        int num = new Integer(s.nextLine()).intValue();
        String[] names = new String[num];
        System.out.println();
        int i = 0;
        while(i < num){
           names[i] = s.nextLine();
           i++;
        }
        StringComparator strcomp = new StringComparator();
        Arrays.sort(names, strcomp);
        for (String name: names){
           System.out.println(name);
        }
     }
  }

   class StringComparator implements Comparator<String>{
      public int compare(String a, String b){
        return a.compareTo(b);
     }
  }

Initial URL

                                

Initial Description
Strings are sorted in ascending order.

This was created by me in order to thwart Dr. McCloskey's evil ways of forcing CS majors to manually alphabetize lists of names and months.

Initial Title
Sorting Strings in Ascending Order

Initial Tags
java

Initial Language
Java