2
votes

I know how to get the current date and time in mfc.but I want to sort the array with the help of date and time datatype.

How can I do this?

Regards,

karthik

1
which date/time class you are using?Naveen
ctime and also coledatetime class ...both class are preferablekarthik
C or C++ (or something else)? I believe the answer is different to the different languages.pmg
As the OP wants C++, I've removed the c tag.pmg

1 Answers

2
votes

CTime has a "<" operator, so you can use std::sort()

void SortTime(CTime vals[], size_t nVals)
{
    std::sort(vals, vals+nVals);
}

If you have an object containing CTimes, you can create your own "<" operator.

struct MyStuff
{
    std::string sName;
    int         nNumber;
    CTime       time;
};

bool operator < (const MyStuff &lhs, const MyStuff &rhs)
{
    return lhs.time < rhs.time;
}

void SortStuff(MyStuff vals[], size_t nVals)
{
    std::sort(vals, vals+nVals);
}

or better

void SortStuff(std::vector<MyStuff> vals)
{
    std::sort(vals.begin(), vals.end());
}