support sorting by a single property

This commit is contained in:
Ari Lazier 2015-09-03 15:46:31 -07:00
parent e2836f77f9
commit beb7c19931
2 changed files with 29 additions and 5 deletions

View File

@ -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<SortOrder>(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");
}

View File

@ -23,8 +23,19 @@
#import <realm/table_view.hpp>
namespace realm {
struct SortOrder {
std::vector<size_t> columnIndices;
std::vector<bool> 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<SortOrder> sort_order;
void setSort(SortOrder s);
};
}