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

W

William Payne

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
 
D

Dave

William Payne said:
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.
 
W

William Payne

Dave said:
William Payne said:
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
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top