In my hypothetical app, I receive a list of hotels from the server.
struct Hotel
{
std::string name; // e.g. Hilton, Ritz
int stars; // In range [0..5], 0 stands for "unrated"
int freeRoomCount; // Number of available rooms [0..N]
int parkingPlacesAvailable; // Number of parking places availalble [0..M]
}
std::vector<Hotel> hotels;
All these items are displayed in a simple List View.
I have to provide different types of sorting. The sorting rules are also dictated by the central server.
The sorting rules are as follows:
std::string sortingRules;
// "s" - sort by stars count
// "f" - sort by free room count
// "p" - sort by parking places available
// The sortingRules string can be a combination of those values. E.g.:
// "ps" - first goes who has the most number of parking places available,
// then goes hotels who has more stars
// More combinations available:
// "s", "sf", "fp", "pf", "spf", "" etc.
// (16 possible combinations, the empty string means alphabetical sorting)
So the question is: how to interpret this in C++? Enums and bit mask values doesn't work since they do not provide the 'order' control.
I am curious how would the community solve this kind of task? I feel like there is an approach for solving this type of problem, that's why I don't want to go straightforward and write code like:
if (sortingRules[0] == "s") ...
I am using C++11 along with Qt 5.4. No boost.