Sort Function

S

SteM

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
 
T

Tim Love

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.
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

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
 
J

Jonathan Mcdougall

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
 
H

Howard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top