@@ -72,6 +72,16 @@ var sortTests = []test{
7272 query : bolthold .Where ("Category" ).Eq ("animal" ).SortBy ("Name" ).Skip (10 ),
7373 result : []int {},
7474 },
75+ {
76+ name : "Sort By Key" ,
77+ query : bolthold .Where ("Category" ).Eq ("animal" ).SortBy ("Key" ),
78+ result : []int {2 , 5 , 8 , 9 , 13 , 14 , 16 },
79+ },
80+ {
81+ name : "Sort By Key Reversed" ,
82+ query : bolthold .Where ("Category" ).Eq ("animal" ).SortBy ("Key" ).Reverse (),
83+ result : []int {16 , 14 , 13 , 9 , 8 , 5 , 2 },
84+ },
7585}
7686
7787func TestSortedFind (t * testing.T ) {
@@ -259,3 +269,45 @@ func TestSortedFindWithNonSlicePtr(t *testing.T) {
259269 _ = store .Find (result , bolthold .Where ("Name" ).Eq ("blah" ).SortBy ("Name" ))
260270 })
261271}
272+
273+ func TestIssue139SortOnSequenceKey (t * testing.T ) {
274+ testWrap (t , func (store * bolthold.Store , t * testing.T ) {
275+
276+ type Row struct {
277+ ID uint64 `boltholdKey:"ID"`
278+ }
279+
280+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
281+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
282+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
283+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
284+
285+ var rows []* Row
286+ ok (t , store .Find (& rows , (& bolthold.Query {}).SortBy ("ID" ).Reverse ()))
287+ equals (t , uint64 (4 ), rows [0 ].ID )
288+ equals (t , uint64 (3 ), rows [1 ].ID )
289+ equals (t , uint64 (2 ), rows [2 ].ID )
290+ equals (t , uint64 (1 ), rows [3 ].ID )
291+
292+ })
293+
294+ testWrap (t , func (store * bolthold.Store , t * testing.T ) {
295+
296+ type Row struct {
297+ ID * uint64 `boltholdKey:"ID"`
298+ }
299+
300+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
301+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
302+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
303+ ok (t , store .Insert (bolthold .NextSequence (), & Row {}))
304+
305+ var rows []* Row
306+ ok (t , store .Find (& rows , (& bolthold.Query {}).SortBy ("ID" ).Reverse ()))
307+ equals (t , uint64 (4 ), * rows [0 ].ID )
308+ equals (t , uint64 (3 ), * rows [1 ].ID )
309+ equals (t , uint64 (2 ), * rows [2 ].ID )
310+ equals (t , uint64 (1 ), * rows [3 ].ID )
311+
312+ })
313+ }
0 commit comments