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

Recommended Answers

All 3 Replies

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.

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.

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());
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.