Solved How to sort a QMap<QString, ...> with a custom string order?
-
Hi,
I have to create a QMap<QString, MyStruct>. The key string is a proprietary coding syntax that do not follow the alphabetical order (too easy otherwise) but has its own order rules. So, I need that my QMap uses this specific syntax. To give you an idea the code is "one letter + number". For the letters I have rules like 'C' < 'A' < 'W' < 'T'. And for the number we are using reverse ordering (12 < 5). And the code is always stored in a QString.So, my problem is to inject this special order in the QMap. I have seen an option, by internally using a QString container class with its own operator<(). But, does it exist an option by just creating the operator<() on QString, like it is possible with std::map?
-
You're trying to fit a square box into a round hole.
QMap
simply doesn't (publicly) support custom comparators. Wrapping the type is an unnecessary overhead. Adding custom operator< in global scope is harmful pollution.QMap
has an undocumented mechanism for customizing comparison function by providing your ownqMapLessThanKey
implementation, but it's still a bad idea.Just use a container suited for the task e.g. std::map that allows for custom comparators.
-
Inherit of QString class and redefine operator<()
-
Hi @semlanik,
As it is a shared development, the inputs I have are QString. So, It is just internally that I can use another type. So, using a struct container or create a subclass of QString is equivalent in this case. -
@Alain38-0
I do not understand whether you are agreeing with @semlanik's solution or not.Sinceand sorts on them, without providing its own comparison operator, it seems to me you will have to follow his suggestion of sub-classingQMap
requiresQString
keysQString
when you put the keys into the map?EDIT Apologies, I must have mis-read
QMap
key type. Ignore me (left here because you responded, deleted my later follow-up). Follow @Pablo-J-Rogina and/or @Chris-Kawa below. -
Hi @JonB,
What I said is, have an internal structure StringContainer embedding a QString and create an operator between StringContainer, or have an internal class SubString : public QString embedding the operator is equivalent. For me, the only difference is that, with the StringContainer I'm sure that the code will never use the QString comparaison operator. -
This post is deleted! -
@Alain38-0 said in How to sort a QMap<QString, ...> with a custom string order?:
The key string is a proprietary coding syntax that do not follow the alphabetical order (too easy otherwise) but has its own order rules. So, I need that my QMap uses this specific syntax.
If it's an "special" string, then create an "special" class to cope with such own order rules. If not, it's a plain QString and it will follow expected order rules.
As @semlanik said, you need to redefine operator<(). From QMap documentation:
In addition, QMap's key type must provide operator<(). QMap uses it to keep its items sorted, and assumes that two keys x and y are equal if neither x < y nor y < x is true.
-
You're trying to fit a square box into a round hole.
QMap
simply doesn't (publicly) support custom comparators. Wrapping the type is an unnecessary overhead. Adding custom operator< in global scope is harmful pollution.QMap
has an undocumented mechanism for customizing comparison function by providing your ownqMapLessThanKey
implementation, but it's still a bad idea.Just use a container suited for the task e.g. std::map that allows for custom comparators.