karthik
karthik

Reputation: 17842

How to sort date and time in mfc?

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

Upvotes: 2

Views: 2136

Answers (1)

Michael J
Michael J

Reputation: 7939

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());
}

Upvotes: 2

Related Questions