Sorting function update

First sort solely determined by date, renamed variables and added
comments