Fix history data sort function