[Qt-interest] Is it a stupid way for sorting?

Malyushytsky, Alex alex at wai.com
Thu Apr 22 08:03:07 CEST 2010


There is no point in sorting any data using widget.
I think you should review help on
void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )
in Qt documentation again.
I think it contains pretty good explanation.

The code below should do the job,  but it is not very efficient

bool myCompareFunction(const QString &s1, const QString &s2)
{
     return s1.count("?") < s2.count("?");
}

QStringList sortPatterns(QStringList patterns)
{
     qSort(patterns.begin(), patterns.end(), myCompareFunction);

     return patterns;
}

It is not very efficient cause every time qSort need to compare 2 values it will recount "?".
Better approach might be to compute it only once.

You might use QMap or build another container like QList<int, QStringList::iterator > and sort it
with above mentioned technique,  ( int value will be a count and iterator will give you access to the string . You can use index instead of iterator )

Example of using map for sorting in your case:

QStringList sortPatterns2(QStringList patterns)
{
     QMultiMap < int, QString> map;

     for(int i=0; i<patterns.count(); i++)
     {
          int count = patterns[i].count("?");
          map.insert( count, patterns[i] );
     }


     return map->values ();
}

Regards,
        Alex


-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of M. Bashir Al-Noimi
Sent: Wednesday, April 21, 2010 7:12 PM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Is it a stupid way for sorting?

Thanks William,

Actually I took this way because I don't know how I can make sort
process by qSort (I found in Qt documentation something about QMap and
qSort), so could you give me an example explains how to do that?

On 21/04/2010 07:58 م, william.crocker at analog.com wrote:
> See:
>
>       qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )
>       et. al.
>
> M. Bashir Al-Noimi wrote:
>
>> Hi All,
>>
>> I want to sort QStringList by specific item count so I tried to use QMap
>> for it but I failed but I used QTableWidget instead but without viewing
>> it in the GUI, *is it correct way for sorting? it's slow (and stupid :-[ )?*
>>
>> QStringList sortPatterns(QStringList patterns)
>> {
>>      QStringList result;
>>      QTableWidget *table = new QTableWidget(patterns.count(), 2);
>>      for(int x=0; x<patterns.count(); x++)
>>      {
>>          int count = patterns[x].count("?");
>>          table->setItem(x, 0, new QTableWidgetItem(QString("%1").arg(count, 2, '0')));
>>          table->setItem(x, 1, new QTableWidgetItem(patterns[x]));
>>      }
>>      table->sortItems(0);
>>      for(int x=0; x<patterns.count(); x++)
>>          result<<table->item(x, 1)->text();
>>      return result;
>> }
>>
>>
>>
>> --
>> Best Regards
>> Muhammad Bashir Al-Noimi
>> My Blog: http://mbnoimi.net
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>

--
Best Regards
Muhammad Bashir Al-Noimi
My Blog: http://mbnoimi.net

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”




More information about the Qt-interest-old mailing list