diff options
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/listview.cc | 30 | ||||
-rw-r--r-- | src/ui/listview.h | 6 |
2 files changed, 21 insertions, 15 deletions
diff --git a/src/ui/listview.cc b/src/ui/listview.cc index 70ecf8f..32d86dd 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -97,22 +97,24 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data) return false; } -void ListView::sort() +bool compare_listitems(const Widget *first, const Widget *second) { - // bubble sort - there's a reason for using it here - for (Children::iterator low = children().begin(); low != children().end(); low++) { - Children::iterator high = low; - for (high++; high != children().end(); high++) { - ListItem *lowitem = dynamic_cast<ListItem *>(*low); - ListItem *highitem = dynamic_cast<ListItem *>(*high); - assert(lowitem && highitem); - if (highitem->sortkey() < lowitem->sortkey()) { - Widget *t = (*low); - (*low) = (*high); - (*high) = t; - } - } + const ListItem *firstitem = dynamic_cast<const ListItem *>(first); + const ListItem *seconditem = dynamic_cast<const ListItem *>(second); + assert(firstitem && seconditem); + + if (firstitem->sortkey() < seconditem->sortkey()) { + return true; + } else if (firstitem->sortkey() > seconditem->sortkey()) { + return false; + } else { + return (firstitem->sortkey().length() < seconditem->sortkey().length()); } } +void ListView::sort() +{ + children().sort(compare_listitems); +} + } diff --git a/src/ui/listview.h b/src/ui/listview.h index f752ac2..81dd5a5 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -59,7 +59,11 @@ public: /// set selection to nothing void deselect(); - /// sort child ListItems according to their sortkey + /** + * @brief sort child ListItems according to their sortkey + * Sort will fail with an assert error if the ListView + * contains any non-ListItem child widgets + */ void sort(); protected: |