Handled sorting of empty Arraylists

Move it to only sort when it is not empty.