Add Composite sorting: OrderBy(...).ThenBy(...) #2632
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces support for multi-key sorting in queries by allowing multiple
OrderByandThenByclauses. The changes update the query structure and pipeline to handle complex ordering, add new methods to the queryable interface, and extend SQL parsing for multiple sort keys. Comprehensive tests have also been added to verify correct behavior. The most important changes are grouped below.Query Structure and Pipeline Updates
Queryclass now supports multiple sort keys by replacing the singleOrderBy/Orderproperties with a list ofQueryOrderobjects. This enables multi-key sorting in queries. (LiteDB/Engine/Query/Query.cs,[LiteDB/Engine/Query/Query.csL20-R20](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-97611a73f837562092daf228e86fdccda3742a76e9e702ccb56856b690f3e344L20-R20))BasePipe,GroupByPipe,QueryPipe) is updated to process multiple sort keys, supporting both single and multi-key sorting logic. (LiteDB/Engine/Query/Pipeline/BasePipe.cs,[[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-6461767771fdec0b3edd95e9e2606bfcdbaf495ec3b4e8989ba07a41d857962bL157-R200);LiteDB/Engine/Query/Pipeline/GroupByPipe.cs,[[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-c886a057f90e8eafb7fb2d8e1d214a8dfbbd7904d6febc27efc5ce544d401c6bL42-R42);LiteDB/Engine/Query/Pipeline/QueryPipe.cs,[[3]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-7e4950815f9584c929339777670c8257ae513f2d3218d4029602618621b530baL49-R49))Queryable Interface and LINQ-style Methods
ILiteQueryable<T>interface and its implementation now includeThenByandThenByDescendingmethods for chaining multiple sort keys, matching LINQ conventions. Error handling is improved for multipleOrderBycalls. (LiteDB/Client/Database/ILiteQueryable.cs,[[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-a07a996a9d353dc6e776893fa3488c5cf7918c0dac423762c1ad2fb3b213659eR23-R26);LiteDB/Client/Database/LiteQueryable.cs,[[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-abeb070f07ebf2ff584f4e6ffdd108467ff216610c0fc2fe25d7330c87ec4523L106-R112),[[3]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-abeb070f07ebf2ff584f4e6ffdd108467ff216610c0fc2fe25d7330c87ec4523L126-R171))SQL Parser Enhancements
ORDER BYkeys, handling comma-separated sort expressions and their respective orders. (LiteDB/Client/SqlParser/Commands/Select.cs,[[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-5e935952ff4227268c8e10fe4d4975a71b01830f62ca156e362623de0abcbd70R129-R130),[[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-5e935952ff4227268c8e10fe4d4975a71b01830f62ca156e362623de0abcbd70L139-R152))Testing and Validation
OrderBy/ThenByclauses. (LiteDB.Tests/Query/OrderBy_Tests.cs,[LiteDB.Tests/Query/OrderBy_Tests.csR109-R182](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-a375641c4901acfdc058526aa00a1470e3c2f181198d394d6339723548fe7201R109-R182))Miscellaneous Improvements
LiteDB/Engine/Query/Query.cs,[[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-97611a73f837562092daf228e86fdccda3742a76e9e702ccb56856b690f3e344L87-R91);LiteDB/Engine/Query/QueryOptimization.cs,[[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-c820145a8c73cc470681110e42d7aa416ef2a4d3259f7ee61df3b54e944dd36bL153-R156),[[3]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-c820145a8c73cc470681110e42d7aa416ef2a4d3259f7ee61df3b54e944dd36bL280-R288))Let me know if you want a deeper walkthrough of any part of the new multi-key sorting implementation!