Sorting using the push and pop function