Added some sorting logic - not perfect, but it will do for now