Sort and limit

Add the ability to sort and limit queries in the read API. This includes a rewrite of how grouping is done so that the sorting and limiting could be shared. It also changes the output format to match the wiki as per discussions during the course of the story.