sorting elements

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Chr
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue May 31, 2005 2:17 pm

sorting elements

Post by Chr »

I've an unlimited number of dates / wxDateTimes (lets say 3-30)
is it possible to add them to a "list" and sort them?
Must i do that for my own, or is there a solution from wxwidgets?
wxWidgets is nice
Luke727
Earned some good credits
Earned some good credits
Posts: 130
Joined: Wed Dec 22, 2004 9:31 am

Post by Luke727 »

Well, there are some comparison functions for wxDateTime, so you should be able to sort them. I suppose you could put them in a wxList and create your own sorting function to sort the list automatically.
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank »

Also, you can use the STL.

Unfortunately the wxDateTime has no operator<(), since the easiest way to sort with the STL is to use operator< just write your own.

Code: Select all

bool operator< (const wxDateTime& lhd, const wxDateTime& rhd)
{
   return lhd.IsEarlierThan(rhd);
}
Now you can use all the algorithms that the STL has at it's disposal. Like using a vector:

Code: Select all

#include <vector>
#include <algorithm>

std::vector<wxDateTime> myDateTimes;
// Put your Datetimes in the Vector. e.g. myDateTimes.push_back(...)

std::sort(myDateTimes.begin(), myDateTimes.end());
Or you can put them in a std::map or std::set, so you have them sorted all the time. Or use a std::list (wich hast std::list::sort). Or put them in an Array and use std::sort on it. Or...
emarti
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 210
Joined: Sat May 07, 2005 8:24 pm
Location: Eskisehir, TURKEY
Contact:

Post by emarti »

wxArrayString::Sort ?????
Post Reply