469,898 Members | 1,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Sort Function

Hi!!

I need something like a "sortfunction" because i want to sort the files
in a directory x by name or date. i can't find any system function in
c++ - any ideas, examples??

Thx,
SteM

Jul 22 '05 #1
5 7537
St**@wic.co.at writes:
Hi!! I need something like a "sortfunction" because i want to sort the files
in a directory x by name or date. i can't find any system function in
c++ - any ideas, examples??

I don't really know what you mean when you say 'something like a
"sortfunction"' or 'i can't find any system function in c++'.

How have you tried to look? Try Google and search for
C++ sort
.. But you'll still need to do some work to get info about files.

Jul 22 '05 #2
St**@wic.co.at wrote:
Hi!!

I need something like a "sortfunction" because i want to sort the files
in a directory x by name or date. i can't find any system function in
c++ - any ideas, examples??

Thx,
SteM


std::list has a sort operation:

void sort ()
Sort the elements.
template<typename StrictWeakOrdering> void sort (StrictWeakOrdering)
Sort the elements according to comparison function.

In addition to that, check out <algorithm>.
I guess there's lots of sorting algos implemented in there.

Regards,
Matthias
Jul 22 '05 #3
> I need something like a "sortfunction" because i want to sort the files
in a directory x by name or date. i can't find any system function in
c++ - any ideas, examples??


Hmm.. what about std::sort?

# include <algorithm>

class File
{
};
void by_name(std::vector<File> &files)
{
std::sort(files.begin(), files.end());
}
bool date_compare(const File &f1, const File &f2)
{
if ( f1.date() < f2.date() )
return true;

return false;
}

void by_date(std::vector<File> &files)
{
std::sort(files.begin(), files.end(), date_compare);
}
Jonathan
Jul 22 '05 #4

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:2d*********************@wagner.videotron.net. ..

bool date_compare(const File &f1, const File &f2)
{
if ( f1.date() < f2.date() )
return true;

return false;
}


How about just:

return (f1.date() < f2.date());

?

-Howard
Jul 22 '05 #5
>>bool date_compare(const File &f1, const File &f2)
{
if ( f1.date() < f2.date() )
return true;

return false;
}
How about just:

return (f1.date() < f2.date());


Thanks for the constructive answer.
Jonathan
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by its me | last post: by
4 posts views Thread by Seeker | last post: by
7 posts views Thread by Christopher Jeris | last post: by
7 posts views Thread by Ireneusz SZCZESNIAK | last post: by
5 posts views Thread by Dr. Ann Huxtable | last post: by
7 posts views Thread by Stuart | last post: by
20 posts views Thread by Xah Lee | last post: by
21 posts views Thread by yeti349 | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.