move sorting logic for history to backend