timshannon / badgerhold

BadgerHold is an embeddable NoSQL store for querying Go types built on Badger
MIT License
514 stars 52 forks source link

Question on default sorting order #81

Closed danvixent closed 2 years ago

danvixent commented 2 years ago

when i do

q := &badgerhold.Query{}
q.SortBy("CreatedAt")

And CreatedAt is unix timestamp, i need to know the order in which badgerhold sorts the data based on that field.

Thanks in advance!

b-tarczynski commented 2 years ago

Hey @danvixent, the default sorting order for badgerhold is ascending. For your case data returned from that query will return the earliest created records first (lowest CreatedAt value). For reversing that behaviour you should call the Reverse function like that: q.SortBy("CreatedAt").Reverse().

timshannon commented 2 years ago

Thanks for answering @b-tarczynski, and everything you say is accurate with a few caveats.

The default order is ascending based on the the byte order, which for the most part works the way you expect, however depending on the type of encoding you use, it may be in an unexpected order.

danvixent commented 2 years ago

Thanks @b-tarczynski @timshannon!