Mixed ascending/descending sort?

I’d like to store records of orders which, like the getting started example, have an order ID, and a price. I’ll then want to query them sorted first by descending price, and then by ascending order ID. So I can’t just use an index of price and order ID and query it in reverse order, because then the order IDs would be backwards.

As best as I can tell, my only option is to write a FunctionKeyExpression which can negate numeric values before they’re stored, and then use that in the index. (And also use it as part of the sort when querying.)

I’ve written that function, and it appears to be working. Now I just want to check, does that seem reasonable? Was there a better way? Will there be any problem with using that function in the primary key index? (I want the records clustered by price, as my most common operation will be “find the X highest paying orders and process some of them”)

I’ve put my code up in a gist in case anyone would care to take a look. https://gist.github.com/jkominek/4d7248b9eba86d39aea5489d2f60f72b

I think that’s a reasonable approach to this problem. The Record Layer doesn’t allow for mixed sort order natively (which is essentially what you need here), so “faking it” by negating numeric types (or at least longs) seems reasonable.

I can’t think of a reason why using it in the primary key expression won’t work. If I had to guess, there are a few places where we try and “match” the results the primary key with other components in an index expression, and some of those might not work (because they were written before FunctionKeyExpressions existed and aren’t quite smart enough to handle it), but I could be wrong. Likewise, sorting by the primary key where a primary key expression includes a function may not work (due to bugs, not due to anything fundamental). But I’d give it a go if I were in your position and see how it does.