Thank you both very much for your help!
You've helped me getting it solved indeed!
My method shall return a QStringList with names. Those names should be put together from first and family name in a user definable order
(either "Gates, Bill" or rather "Steve Jobs") and a sorting depending on first or family name.
I simply canNOT use a QMap for such a sorting job, which I did (not knowing alternatives at that time).
I'd actually constructed a really messy and overly complicated thing in the first place.
Being fairly new to OOP I'd missed the obvious thing to do: create a "Name" class with some getter and setter methods and an overloaded operator< and then do a simple sort on a QList<Name>.
According to your ideas and the QMap docs I've implemented it like this way:
const inline bool operator<(const Name &n1, const Name &n2)
{
int comp;
if (n1.sortingOrder()==Name::familyFirst)
{
comp
=QString::localeAwareCompare(n1.
family(),n2.
family());
if (comp!=0)
return comp < 0;
comp
=QString::localeAwareCompare(n1.
first(),n2.
first());
if (comp!=0)
return comp < 0;
comp
=QString::localeAwareCompare(n1.
id(),n2.
id());
return comp<0;
} else {
comp
=QString::localeAwareCompare(n1.
first(),n2.
first());
if (comp!=0)
return comp < 0;
comp
=QString::localeAwareCompare(n1.
family(),n2.
family());
if (comp!=0)
return comp < 0;
comp
=QString::localeAwareCompare(n1.
id(),n2.
id());
return comp<0;
}
}
const inline bool operator<(const Name &n1, const Name &n2)
{
int comp;
if (n1.sortingOrder()==Name::familyFirst)
{
comp=QString::localeAwareCompare(n1.family(),n2.family());
if (comp!=0)
return comp < 0;
comp=QString::localeAwareCompare(n1.first(),n2.first());
if (comp!=0)
return comp < 0;
comp=QString::localeAwareCompare(n1.id(),n2.id());
return comp<0;
} else {
comp=QString::localeAwareCompare(n1.first(),n2.first());
if (comp!=0)
return comp < 0;
comp=QString::localeAwareCompare(n1.family(),n2.family());
if (comp!=0)
return comp < 0;
comp=QString::localeAwareCompare(n1.id(),n2.id());
return comp<0;
}
}
To copy to clipboard, switch view to plain text mode
I now build a list of this class and just sort it:
QList<Name> nameList;
//fill the list, e.g. like this, but in a loop:
//nameList.append(Name(first, family, id);
//nameList.last().setSortingOrder(Name::familyFirst);
qSort(nameList);
QList<Name> nameList;
//fill the list, e.g. like this, but in a loop:
//nameList.append(Name(first, family, id);
//nameList.last().setSortingOrder(Name::familyFirst);
qSort(nameList);
To copy to clipboard, switch view to plain text mode
This has *so* many advantages over my old approach...
Thank you!
Bookmarks