From beb7c19931751c91223c0abcff3c9097127d6f86 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 3 Sep 2015 15:46:31 -0700 Subject: [PATCH] support sorting by a single property --- results.cpp | 18 ++++++++++++++---- results.hpp | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/results.cpp b/results.cpp index 6b240f59..f33fc465 100644 --- a/results.cpp +++ b/results.cpp @@ -21,17 +21,26 @@ using namespace realm; -Results::Results(SharedRealm &r, ObjectSchema &o, Query q) : +Results::Results(SharedRealm &r, ObjectSchema &o, Query q, SortOrder s) : realm(r), object_schema(o), backing_query(q), table_view(backing_query.find_all()) { + setSort(std::move(s)); } -size_t Results::size() { +size_t Results::size() +{ verify_attached(); return table_view.size(); } -Row Results::get(std::size_t row_ndx) { +void Results::setSort(SortOrder s) +{ + sort_order = std::make_unique(std::move(s)); + table_view.sort(sort_order->columnIndices, sort_order->ascending); +} + +Row Results::get(std::size_t row_ndx) +{ verify_attached(); if (row_ndx >= table_view.size()) { throw std::range_error(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." + @@ -40,7 +49,8 @@ Row Results::get(std::size_t row_ndx) { return table_view.get(row_ndx); } -void Results::verify_attached() { +void Results::verify_attached() +{ if (!table_view.is_attached()) { throw std::runtime_error("Tableview is not attached"); } diff --git a/results.hpp b/results.hpp index a58136c9..fcb3e894 100644 --- a/results.hpp +++ b/results.hpp @@ -23,8 +23,19 @@ #import namespace realm { + struct SortOrder { + std::vector columnIndices; + std::vector ascending; + + explicit operator bool() const { + return !columnIndices.empty(); + } + }; + + static SortOrder s_defaultSort = {{}, {}}; + struct Results { - Results(SharedRealm &r, ObjectSchema &o, Query q); + Results(SharedRealm &r, ObjectSchema &o, Query q, SortOrder s = s_defaultSort); size_t size(); Row get(std::size_t row_ndx); void verify_attached(); @@ -33,6 +44,9 @@ namespace realm { ObjectSchema &object_schema; Query backing_query; TableView table_view; + std::unique_ptr sort_order; + + void setSort(SortOrder s); }; }