From 920d252baf5b223c333cda05e4c6e9a399f358b3 Mon Sep 17 00:00:00 2001 From: Philipp Zabel Date: Fri, 2 Jul 2010 18:36:15 +0200 Subject: [PATCH] Add custom sorting functions for time and price strings A simple lexical sort won't do the right thing. --- src/lift-list-window.vala | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/lift-list-window.vala b/src/lift-list-window.vala index 4c59c5e..2e5d6ef 100644 --- a/src/lift-list-window.vala +++ b/src/lift-list-window.vala @@ -68,6 +68,8 @@ public class LiftListWindow : StackableWindow { gconf = GConf.Client.get_default (); store = new ListStore (7, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (Lift)); sortable = new TreeModelSort.with_model (store); + sortable.set_sort_func (Columns.DATETIME, datetime_sort_func); + sortable.set_sort_func (Columns.PRICE, price_sort_func); alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f); alignment.top_padding = MARGIN_HALF; @@ -185,6 +187,49 @@ public class LiftListWindow : StackableWindow { }); } + private int datetime_sort_func (TreeModel model, TreeIter a, TreeIter b) { + Lift la; + Lift lb; + model.get (a, Columns.LIFT, out la); + model.get (b, Columns.LIFT, out lb); + int year = la.time.year - lb.time.year; + if (year != 0) + return year; + int month = la.time.month - lb.time.month; + if (month != 0) + return month; + int day = la.time.day - lb.time.day; + if (day != 0) + return day; + int hour = la.time.hour - lb.time.hour; + // Negative hour means no exact time available + // - sort those entries last + if (la.time.hour < 0) { + hour += 24; + return hour; + } + if (lb.time.hour < 0) { + hour -= 24; + return hour; + } + if (hour != 0) + return hour; + int minute = la.time.minute - lb.time.minute; + return minute; + } + + private int price_sort_func (TreeModel model, TreeIter a, TreeIter b) { + string pa; + string pb; + model.get (a, Columns.PRICE, out pa); + model.get (b, Columns.PRICE, out pb); + if (pa == null && pb != null) + return 1; + if (pa != null && pb == null) + return -1; + return strcmp (pa, pb); + } + private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) { unowned string text; CellRendererText renderer; -- 1.7.9.5