Sorting vector of structs depending on different data members for each sort

Discussion in 'C++' started by William Payne, Apr 20, 2004.

  1. Hello, I have two structs:

    struct FileEntry
    {
    FileEntry(const char* name,
    const char* type,
    std::size_t file_size,
    HICON icon)
    :
    m_file_size(file_size),
    m_icon(icon)
    {
    std::strcpy(m_name, name);
    std::sprintf(m_file_size_as_string, "%u KB", m_file_size / 1024);
    }

    char m_name[MAX_PATH + 1];
    char m_type[64];
    std::size_t m_file_size;
    char m_file_size_as_string[32];
    HICON m_icon;
    };

    struct FolderEntry
    {
    FolderEntry(const char* name,
    const char* type,
    HICON icon)
    :
    m_icon(icon)
    {
    std::strcpy(m_name, name);
    std::strcpy(m_type, type);
    }

    char m_name[MAX_PATH + 1];
    char m_type[64];
    HICON m_icon;
    };

    and in my program I have two std::vector, one containing FileEntry one
    containing FolderEntry. I need to sort these vectors on a single member
    variable in each struct. The FileEntry struct may be sorted depending on
    name, size or type. The FolderEntry struct may be sorted on name or type. So
    how do I do this on the best way? I cannot overload the comparison operator
    because I don't know which data member I will base may sort on, or can I?
    Please advise on how I should proceed.

    Thanks for any replies

    / WP
    William Payne, Apr 20, 2004
    #1
    1. Advertising

  2. William Payne

    Dave Guest

    "William Payne" <> wrote in message
    news:c64227$ljc$...
    > Hello, I have two structs:
    >
    > struct FileEntry
    > {
    > FileEntry(const char* name,
    > const char* type,
    > std::size_t file_size,
    > HICON icon)
    > :
    > m_file_size(file_size),
    > m_icon(icon)
    > {
    > std::strcpy(m_name, name);
    > std::sprintf(m_file_size_as_string, "%u KB", m_file_size / 1024);
    > }
    >
    > char m_name[MAX_PATH + 1];
    > char m_type[64];
    > std::size_t m_file_size;
    > char m_file_size_as_string[32];
    > HICON m_icon;
    > };
    >
    > struct FolderEntry
    > {
    > FolderEntry(const char* name,
    > const char* type,
    > HICON icon)
    > :
    > m_icon(icon)
    > {
    > std::strcpy(m_name, name);
    > std::strcpy(m_type, type);
    > }
    >
    > char m_name[MAX_PATH + 1];
    > char m_type[64];
    > HICON m_icon;
    > };
    >
    > and in my program I have two std::vector, one containing FileEntry one
    > containing FolderEntry. I need to sort these vectors on a single member
    > variable in each struct. The FileEntry struct may be sorted depending on
    > name, size or type. The FolderEntry struct may be sorted on name or type.

    So
    > how do I do this on the best way? I cannot overload the comparison

    operator
    > because I don't know which data member I will base may sort on, or can I?
    > Please advise on how I should proceed.
    >
    > Thanks for any replies
    >
    > / WP
    >
    >

    Create custom sorting criteria in the form of function objects and pass one
    of them to std::sort(). You'll need to define one for each way in which you
    wish to sort. A given function object will do nothing more than compare the
    field of interest of two of your structs.
    Dave, Apr 20, 2004
    #2
    1. Advertising

  3. "Dave" <> wrote in message
    news:...
    >
    > "William Payne" <> wrote in message
    > news:c64227$ljc$...
    > > Hello, I have two structs:
    > >
    > > struct FileEntry
    > > {
    > > FileEntry(const char* name,
    > > const char* type,
    > > std::size_t file_size,
    > > HICON icon)
    > > :
    > > m_file_size(file_size),
    > > m_icon(icon)
    > > {
    > > std::strcpy(m_name, name);
    > > std::sprintf(m_file_size_as_string, "%u KB", m_file_size / 1024);
    > > }
    > >
    > > char m_name[MAX_PATH + 1];
    > > char m_type[64];
    > > std::size_t m_file_size;
    > > char m_file_size_as_string[32];
    > > HICON m_icon;
    > > };
    > >
    > > struct FolderEntry
    > > {
    > > FolderEntry(const char* name,
    > > const char* type,
    > > HICON icon)
    > > :
    > > m_icon(icon)
    > > {
    > > std::strcpy(m_name, name);
    > > std::strcpy(m_type, type);
    > > }
    > >
    > > char m_name[MAX_PATH + 1];
    > > char m_type[64];
    > > HICON m_icon;
    > > };
    > >
    > > and in my program I have two std::vector, one containing FileEntry one
    > > containing FolderEntry. I need to sort these vectors on a single member
    > > variable in each struct. The FileEntry struct may be sorted depending on
    > > name, size or type. The FolderEntry struct may be sorted on name or

    type.
    > So
    > > how do I do this on the best way? I cannot overload the comparison

    > operator
    > > because I don't know which data member I will base may sort on, or can

    I?
    > > Please advise on how I should proceed.
    > >
    > > Thanks for any replies
    > >
    > > / WP
    > >
    > >

    > Create custom sorting criteria in the form of function objects and pass

    one
    > of them to std::sort(). You'll need to define one for each way in which

    you
    > wish to sort. A given function object will do nothing more than compare

    the
    > field of interest of two of your structs.
    >
    >


    Thanks, works great. I just noted that I cannot overload the name of the
    function used for sorting. I.E., I can't have:

    bool sort_on_name(const FileEntry& lhs, const FileEntry& rhs);

    bool sort_on_name(const FolderEntry& lhs, const FolderEntry& rhs);

    instead, I had to use unique names:

    bool sort_files_on_name(const FileEntry& lhs, const FileEntry& rhs);

    bool sort_folders_on_name(const FolderEntry& lhs, const FolderEntry& rhs);

    / WP
    William Payne, Apr 20, 2004
    #3
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    633
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    555
    Chris Hauxwell
    Apr 27, 2004
  3. Replies:
    8
    Views:
    1,913
    Csaba
    Feb 18, 2006
  4. Kit
    Replies:
    0
    Views:
    337
  5. Igor Nn
    Replies:
    7
    Views:
    435
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page