# Mixed ascending/descending sort?

**URL:** <https://forums.foundationdb.org/t/mixed-ascending-descending-sort/1582>\
**Category:** Record Layer\
**Created:** [August 12, 2019, 9:35pm UTC](https://forums.foundationdb.org/t/mixed-ascending-descending-sort/1582 "2019-08-12T21:35:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkominek](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/jkominek/32/140_2.png) [@jkominek](https://forums.foundationdb.org/u/jkominek)\
**Post date:** [August 12, 2019, 9:35pm UTC](https://forums.foundationdb.org/t/mixed-ascending-descending-sort/1582/1 "2019-08-12T21:35:24Z")

</div>

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](https://gist.github.com/jkominek/4d7248b9eba86d39aea5489d2f60f72b)

---

<div class="post-metadata">

**Author:** ![alloc](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/alloc/32/9_2.png) [@alloc](https://forums.foundationdb.org/u/alloc)\
**Post date:** [August 12, 2019, 11:19pm UTC](https://forums.foundationdb.org/t/mixed-ascending-descending-sort/1582/2 "2019-08-12T23:19:20Z")

</div>

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 `long`s) 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.
