[Qt-interest] Simple qSort() question
Joshua Grauman
jnfo-c at grauman.com
Thu Jan 13 05:17:57 CET 2011
I'm sorry, but I think I must really be missing something here. That's
what I'd like to achieve (I already have a lessThan function written), but
don't see how to do that. As soon as I sort my first QStringList, the
QList<int> no longer points to the members of that QStringList... That's
the problem, that I can't figure out how to sort a list AND indexing
information together (at least easily). I guess I could create my own
class and reimplement all the necessary functions, but was trying to avoid
that if there was a simpler way. Thanks again.
Josh
>
>
> Joshua Grauman wrote:
>> No'am,
>>
>> Thanks for the reply. I know I could use the method you outlined, but then
>> I'd need to create my own sort.
>
> You don't.
> You only have to write your own lessThan.
>
> void qSort ( begin, end, lessThan )
>
>> Not a big deal, but since qSort is already
>> optimized/etc, I was just seeing if there were some Qt specific tricks,
>> functions, classes, etc. that would make this easy for me that I didn't
>> know of.
>>
>> Thanks again.
>>
>> Josh
>>
>>>
>>>> -----Original Message-----
>>>> From: qt-interest-bounces+noam.rosenthal=nokia.com at qt.nokia.com
>>>> [mailto:qt-interest-bounces+noam.rosenthal=nokia.com at qt.nokia.com] On
>>>> Behalf Of ext Joshua Grauman
>>>> Sent: Wednesday, January 12, 2011 4:03 PM
>>>> To: qt-interest at qt.nokia.com
>>>> Subject: [Qt-interest] Simple qSort() question
>>>>
>>>> Hello all,
>>>>
>>>> I have three QStringLists that I would like to sort. They all are
>>>> guarenteed to have the same number of items in them with rows that
>>>> correspond to each other. I often want to sort my data based on one of
>>>> the
>>>> lists. It's easy enough to sort a single list, but then the data in the
>>>> other two lists doesn't match. Is there an easy way to do this that I'm
>>>> not thinking of?
>>> Have a fourth list of indices only (e.g. QList<int>) or a structure that contains both the int and the string, and sort it using the first list's data.
>>> Then when you need any ordered data, you go through this indexed list, and fetch the values from the original 3 lists, e.g.
>>>
>>> For (int i = 0; i < indices.size(); ++i) {
>>> int index = indices[i];
>>> QString s1 = list1[index];
>>> QString s2 = list2[index];
>>> QString s3 = list3[index];
>>> }
>>>
>>> Btw this is not a Qt question, it's more of a general data-model question :)
>>>
>>> No'am
>>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list