Sort Function

Discussion in 'C++' started by SteM@wic.co.at, Nov 23, 2004.

  1. Guest

    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
    , Nov 23, 2004
    #1
    1. Advertising

  2. Tim Love Guest

    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.
    Tim Love, Nov 23, 2004
    #2
    1. Advertising

  3. 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
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 23, 2004
    #3
  4. > 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
    Jonathan Mcdougall, Nov 23, 2004
    #4
  5. Howard Guest

    "Jonathan Mcdougall" <> wrote in message
    news:2dMod.49505$...

    >
    > 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
    Howard, Nov 23, 2004
    #5
  6. >>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
    Jonathan Mcdougall, Nov 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    535
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,400
    Dave Moore
    Apr 28, 2004
  3. =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=

    [B,IX] = sort(A,...) - Order for sort()-function

    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=, May 29, 2007, in forum: Python
    Replies:
    0
    Views:
    319
    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=
    May 29, 2007
  4. Navin
    Replies:
    1
    Views:
    685
    Ken Schaefer
    Sep 9, 2003
  5. GIMME
    Replies:
    5
    Views:
    185
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page