Use constuctor and prototype pattern and make sort non-mutating

Since the tests unfortunately force exposing `db`, there is really no
private-information hiding reason not to use the constructor pattern, so
I decided to make that change. Also, I decided to make sort a pure query
method so that it does not mutate the internal state, even though that
state can easily be mutated via the `db` property.

Note that it would be better if `sort` was named `sorted` or something
like that so that it was clear that it is a query method. :/