Sort and consolidate ranges. For #1754