fixed sort to consider date value + added missing var declaration