You should be able to put your objects (apples, 3) (oranges, 2) (bananas, 5) into a List and then call Collections.sort(yourlist). You'd then want to make sure the object you declared implements the Comparable interface.
More information is available at http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
Let's say you declared you object as
import java.util.*;
public class FruitAndCount implements Comparable<FruitAndCount> {
private final String _name;
private final int _count;
public FruitAndCount(String name, int count) {
this._name = name;
this._count = count;
}
public String name() { return _name; }
public String count() { return _count; }
public int compareTo(FruitAndCount comp) {
return this._count.compareTo(comp.count);
}
}
You should then be able to make the following call which will sort your list:
FruitAndCount fruitArray[] = {
new FruitAndCount("Apples", 3),
new FruitAndCount("Oranges", 2),
new FruitAndCount("Bananas", 5)
};
List<FruitAndCount> fruit = Arrays.asList(fruitArray);
Collections.sort(fruit);
You should then have a sorted list of fruit.
Warning: Syntax errors may be present. I have not tested any of the above code.