W
William Payne
Hello, in my program I have a std::vector of structs, a struct such as:
enum Type
{
TYPE_FILE,
TYPE_FOLDER
};
struct Entry
{
Entry(const char* name,
const char* description,
long file_size,
Type type,
HICON icon)
:
m_file_size(file_size),
m_type(type),
m_icon(icon)
{
std::memset(m_name, '\0', sizeof(m_name));
std::memset(m_description, '\0', sizeof(m_description));
std::memset(m_file_size_as_string, '\0',
sizeof(m_file_size_as_string));
std::strcpy(m_name, name);
if(m_type == TYPE_FILE)
{
std::sprintf(m_file_size_as_string, "%li KB", m_file_size);
}
}
char m_name[MAX_PATH + 1];
char m_description[64];
long m_file_size;
char m_file_size_as_string[32];
Type m_type;
HICON m_icon;
};
As you can see, each Entry can be either a TYPE_FILE or a TYPE_FOLDER. Now I
need to sort this vector, and I am beginning with sorting on names. But
there is a catch, an Entry of type TYPE_FOLDER is always less than an Entry
of type TYPE_FILE. So if I sort my vector in descending order, I want all
elements of TYPE_FOLDER to come first (sorted by name), and then all
elements of TYPE_FILE (sorted by name). If sorted by ascending order I want
all elements of TYPE_FILE first (sorted by name) followed by all elements of
type TYPE_FOLDER (sorted by name). (Yes, I am trying to mimic Windows XP
explorer.) How do I do that? I tried the following which seems to work for
ascending order, but for descending order I get all folders first when they
should be last. Please help me solve this!
bool sort_on_name(const Entry& lhs, const Entry& rhs)
{
if(lhs.m_type == TYPE_FOLDER && rhs.m_type == TYPE_FILE)
{
return true;
}
if(lhs.m_type == TYPE_FILE && rhs.m_type == TYPE_FOLDER)
{
return false;
}
return std::strcmp(lhs.m_name, rhs.m_name);
}
std::stable_sort(entries.begin(), entries.end(), sort_on_name);
Thanks for any replies
/ WP
enum Type
{
TYPE_FILE,
TYPE_FOLDER
};
struct Entry
{
Entry(const char* name,
const char* description,
long file_size,
Type type,
HICON icon)
:
m_file_size(file_size),
m_type(type),
m_icon(icon)
{
std::memset(m_name, '\0', sizeof(m_name));
std::memset(m_description, '\0', sizeof(m_description));
std::memset(m_file_size_as_string, '\0',
sizeof(m_file_size_as_string));
std::strcpy(m_name, name);
if(m_type == TYPE_FILE)
{
std::sprintf(m_file_size_as_string, "%li KB", m_file_size);
}
}
char m_name[MAX_PATH + 1];
char m_description[64];
long m_file_size;
char m_file_size_as_string[32];
Type m_type;
HICON m_icon;
};
As you can see, each Entry can be either a TYPE_FILE or a TYPE_FOLDER. Now I
need to sort this vector, and I am beginning with sorting on names. But
there is a catch, an Entry of type TYPE_FOLDER is always less than an Entry
of type TYPE_FILE. So if I sort my vector in descending order, I want all
elements of TYPE_FOLDER to come first (sorted by name), and then all
elements of TYPE_FILE (sorted by name). If sorted by ascending order I want
all elements of TYPE_FILE first (sorted by name) followed by all elements of
type TYPE_FOLDER (sorted by name). (Yes, I am trying to mimic Windows XP
explorer.) How do I do that? I tried the following which seems to work for
ascending order, but for descending order I get all folders first when they
should be last. Please help me solve this!
bool sort_on_name(const Entry& lhs, const Entry& rhs)
{
if(lhs.m_type == TYPE_FOLDER && rhs.m_type == TYPE_FILE)
{
return true;
}
if(lhs.m_type == TYPE_FILE && rhs.m_type == TYPE_FOLDER)
{
return false;
}
return std::strcmp(lhs.m_name, rhs.m_name);
}
std::stable_sort(entries.begin(), entries.end(), sort_on_name);
Thanks for any replies
/ WP