Fix primitive sort ordering and add tests for class-based sorting.