Fix bug where dates where sorted incorrectly and out of index error