1. make a class to contain your values 2. let that class implement the Comparable interface 3. implement the compareTo() method with your custom sorting Now, by implementing Comparable, you can use some extra collection features such as this sort method for lists (that includes ArrayList) or )"]this one for simple arrays There are more ways to do it, like comparators, but they are functionally similar (and not any easier anyway). You should avoid writing your own sorting methods, as the java collection framework is already optimized.