Sort: Delegate UDA string sorting to Variant::operator<

This commit is contained in:
Paul Beckingham 2015-04-04 12:53:06 -04:00
parent 8c0f46309b
commit 370afa0d26

View file

@ -30,6 +30,7 @@
#include <string> #include <string>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Variant.h>
#include <Duration.h> #include <Duration.h>
#include <Task.h> #include <Task.h>
#include <text.h> #include <text.h>
@ -67,6 +68,7 @@ void sort_tasks (
// require re-parsing. // require re-parsing.
// //
// Essentially a static implementation of a dynamic operator<. // Essentially a static implementation of a dynamic operator<.
// UDA string values delegate to Variant::operator<.
static bool sort_compare (int left, int right) static bool sort_compare (int left, int right)
{ {
std::string field; std::string field;
@ -245,16 +247,19 @@ static bool sort_compare (int left, int right)
return ascending ? (left_real < right_real) return ascending ? (left_real < right_real)
: (left_real > right_real); : (left_real > right_real);
} }
// UDA values of type 'string' are sorted by Variant::operator<.
// By setting 'source' to the UDA name, the comparison operator can use
// the custom sort order, if defined.
else if (type == "string") else if (type == "string")
{ {
const std::string& left_string = (*global_data)[left].get_ref (field); Variant l ((*global_data)[left].get_ref (field));
const std::string& right_string = (*global_data)[right].get_ref (field); Variant r ((*global_data)[right].get_ref (field));
if (l == r)
if (left_string == right_string)
continue; continue;
return ascending ? (left_string < right_string) l.source (field);
: (left_string > right_string); r.source (field);
return ascending ? (l < r) : (r < l);
} }
else if (type == "date") else if (type == "date")
{ {