View Full Version : Figuring out what date is more recent than the other?

20th March 2011, 17:45
Hello all.
I think my problem is quite complex.
My program saves history of the actions the user did. I like the way it does this, and I wouldn't like to change it, actually.
The problem is that it takes some filenames from a directory named, e.g.
Wednesday 03.16.2011 and simply adds them to a treewidget.
Unfortunately, it adds them in random order, but, what I want is e.g.
the Tuesday 03.01.2011 to be 1st, then Thursday 03.03.2011 to be second and Sunday 03.20.2011 to be third.
I have to mention that only the days do count, because I make another parent for another month. So, actually, I have to compare the value between the two dots (03.01.2011) and then do a binary search and figure out how to make the list as I want and then add to the treewidget....

How can I extract the value I want ?
I'm thinking making a float a[31]; and adding there as I wish the values I want, but even if I have the values in there as I want e.g.
a[4]=26 etc
How can I now which values matches with each item in order to place them correctly?

20th March 2011, 17:57
How do you add items to the tree?

20th March 2011, 18:16
I'm making a parent (which is the month) and..

20th March 2011, 18:18
So why don't you just add the item in the proper place using QTreeWidgetItem::insertChild() instead of adding it at the end of the list? You don't need to parse anything, you can store the date as a value for a custom role in the item.

20th March 2011, 18:27
Also, note that you can impose a strict ordering on dates with the information you already have if you order them by YYYYMMDD, with single digit values left-padded with zeros. This format is monotonically increasing for all dates.

Or, if your dates are guaranteed to lie within the Unix epoch (> ~1970?) you can simply use a system or Qt routine to convert the calendar date into a Unix-style timestamp, which will again be a monotonically increasing series of integer values.

Either way, you can trivially order your filenames by implementing a comparison function and applying a sort routine.

20th March 2011, 18:30
Build yourself a function int extractDay(QString date). Use a (static) QRegExp (something like \d+\.(\d+)\.\d+ ) and its capturing text functionality (http://doc.trolltech.com/latest/qregexp.html#capturing-text) to extract the relevant string and convert that to int with QString::toInt().

When inserting, walk through the already inserted siblings and move forward only when the extracted day is greater than the one of the other item.. This results in a bubble sort.

Let me know if you need more details,



20th March 2011, 18:58
Thx to all, I'll try all the solutions you gave, and if I don't make it, I'll post back :)