1,105,214 Community Members

sorting strings into alphabetical order

Member Avatar
lil_panda
Light Poster
33 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

How would I sort an array or arrayList of strings into alphabetical order?

Member Avatar
Alex Edwards
Posting Shark
971 posts since Jun 2008
Reputation Points: 321 [?]
Q&As Helped to Solve: 109 [?]
Skill Endorsements: 2 [?]
 
0
 

How would I sort an array or arrayList of strings into alphabetical order?

You could do something like this--

import java.util.*;

public class Sort_Kit{

	private final static int NUMBERS = 10;
	private static int passes = 0;

	public static void main(String[] args){
		ArrayList<String> strings = new ArrayList<String>(0);
		Random rgen = new Random();

		for(int i = 0; i < NUMBERS; i++)
			strings.add("" + (rgen.nextInt(999)%10));

		System.out.println(strings);
		try{Sort_Kit.<String>sortArrayList(strings, true);}catch(Throwable t){}
		System.out.println(strings);
		System.out.println("Sorted in " + passes + " passes!");
	}

	static <T extends Comparable<T> > void sortArrayList(ArrayList<T> arg, boolean order) throws Exception{
		int turn = 0;
		while(!Sort_Kit.<T>isSorted(arg)){
			for(int i = 0; i < arg.size() - 1; i++){
				T temp = (order)
				? (arg.get(i).compareTo(arg.get(i + 1)) <  arg.get(i + 1).compareTo(arg.get(i))
				? (arg.get(i)) : (arg.get(i + 1)) ) : ((arg.get(i).compareTo(arg.get(i + 1)) <  arg.get(i + 1).compareTo(arg.get(i)) )
				? (arg.get(i)) : (arg.get(i + 1))),
				 temp2 = (order)
				? (arg.get(i).compareTo(arg.get(i + 1)) >  arg.get(i + 1).compareTo(arg.get(i))
				? (arg.get(i)) : (arg.get(i + 1)) ) : ((arg.get(i).compareTo(arg.get(i + 1)) <  arg.get(i + 1).compareTo(arg.get(i)) )
				? (arg.get(i)) : (arg.get(i + 1)));
				arg.set(i, temp);
				arg.set(i + 1, temp2);
				System.out.println(temp + "  " + temp2);
				Thread.sleep(250); // for debug purposes
			}
			System.out.println(arg);
			turn++;
		}
              passes = turn;
	}

	private static <T extends Comparable<T> > boolean isSorted(ArrayList<T> arg){
		int count = 0;
		for(int i = 0; i < arg.size() - 1; i++)
			count = (arg.get(i).compareTo(arg.get(i + 1)) <= arg.get(i + 1).compareTo(arg.get(i))) ? ++count: count;

		System.out.println(count);
		return count == (arg.size() - 1);
	}
}

The example uses numbers as Strings but you can use regular Strings as well.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

Use the Arrays class: Arrays

Signature:
public static void sort(Object[] a)

String [] arr=new String[5];
//put stuff into the array.

Arrays.sort(arr);

The sort will take any array with Objects as long they implement the Comparable interface and they can be compared with each other.
The String object does implement the Comparable interface. It is better the array you use as input to have objects of the same type.

You can also sort integers:

A) as primitive types:

int [] arr=new int[5];
//put stuff into the array.

Arrays.sort(arr);

A) as objects:

Integer [] arr=new Integer[5];
//put stuff into the array.

Arrays.sort(arr);

The Integer object does implement the Comparable interface.

Member Avatar
gregoryalussier
Newbie Poster
2 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

In case someone wants to sort an objects ArrayList using a criteria. In my case, I had User objects, and would like to sort it by username. The getUsername() method returns the username's String.

Where you want it to be sorted, supposing your ArrayList is named "users".

Collections.sort(users, new byUsername());

The comparator class

public class byUsername implements Comparator
{
    public int compare(Object o1, Object o2)
    {
        User u1 = (User) o1;
        User u2 = (User) o2;
        return u1.getUsername().compareTo(u2.getUsername());
    }
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article