fix history sorting function