Some java code for implementing sorting at the end