Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, February 2007
Basic QStringList sort question


Message 1 in thread

Thank you for reading this.

I need to sort the members of a QStringList in QString length order (longest
words first). The documentation mentions using QMap <int> <QString> for
nonstandard sorts but I can't see how this is done. Can someone point me in
the right direction?

I started to code a bubble sort method but I'd like to see how Qt handles
unusual sorts.

Regards,
Phil.

--
 [ signature omitted ] 

Message 2 in thread

On 2/18/07, Phil <phillor@xxxxxxxxxxx> wrote:
> Thank you for reading this.
>
> I need to sort the members of a QStringList in QString length order (longest
> words first). The documentation mentions using QMap <int> <QString> for
> nonstandard sorts but I can't see how this is done. Can someone point me in
> the right direction?
>
> I started to code a bubble sort method but I'd like to see how Qt handles
> unusual sorts.

One of the "sort" algorithm overloads from standart C++ library,
accepts custom comparation functor:
http://www.sgi.com/tech/stl/sort.html

The code using it would look something like this (don't take it
literraly - I didn't compile it):

template<typename T>
struct LengthComp
{
	bool operator()(const T& first,const T& second) {
		return first.length() < seond.length();
	}	
};

QStringList list;
//...

std::sort(list.begin(), list.end(), LengthComp<QString>());

-- 
 [ signature omitted ] 

Message 3 in thread

On 2/18/07, Pavel Antokolsky aka Zigmar <zigmar@xxxxxxxxx> wrote:
> On 2/18/07, Phil <phillor@xxxxxxxxxxx> wrote:
> > Thank you for reading this.
> >
> > I need to sort the members of a QStringList in QString length order (longest
> > words first). The documentation mentions using QMap <int> <QString> for
> > nonstandard sorts but I can't see how this is done. Can someone point me in
> > the right direction?
> >
> > I started to code a bubble sort method but I'd like to see how Qt handles
> > unusual sorts.
>
> One of the "sort" algorithm overloads from standart C++ library,
> accepts custom comparation functor:
> http://www.sgi.com/tech/stl/sort.html

And there is also qSort(), available if you have access to stl.
http://doc.trolltech.com/4.2/qtalgorithms.html#qSort

-- 
 [ signature omitted ] 

Message 4 in thread

On 2/18/07, Robin Ericsson <lobbin@xxxxxxxxx> wrote:
> On 2/18/07, Pavel Antokolsky aka Zigmar <zigmar@xxxxxxxxx> wrote:
> > On 2/18/07, Phil <phillor@xxxxxxxxxxx> wrote:
> > > I need to sort the members of a QStringList in QString length order (longest
> > > words first). The documentation mentions using QMap <int> <QString> for
> > > nonstandard sorts but I can't see how this is done. Can someone point me in
> > > the right direction?
> > One of the "sort" algorithm overloads from standart C++ library,
> > accepts custom comparation functor:
> > http://www.sgi.com/tech/stl/sort.html
> And there is also qSort(), available if you have access to stl.
> http://doc.trolltech.com/4.2/qtalgorithms.html#qSort
The qSort uses type's built-in comparison (operator<()), and does not
allows to specify custom comparator, as the STL algorithm I've
mentioned. If qSort will be called on QStringList, they will be sorted
alphanumerically and not by length, as Phil wants.

-- 
 [ signature omitted ] 

Message 5 in thread

Hi,

Pavel Antokolsky aka Zigmar wrote:
>> And there is also qSort(), available if you have access to stl.
>> http://doc.trolltech.com/4.2/qtalgorithms.html#qSort
> The qSort uses type's built-in comparison (operator<()), and does not
> allows to specify custom comparator, as the STL algorithm I've
> mentioned. If qSort will be called on QStringList, they will be sorted
> alphanumerically and not by length, as Phil wants.

There is more that one version of qSort: one takes just a container 
reference, one takes start & end iterators and finally one takes start & 
end iterators and a compare function/function object.

Tim

--
 [ signature omitted ] 

Message 6 in thread

On 2/18/07, Tim Dewhirst <tim@xxxxxxxxxxxxx> wrote:
> There is more that one version of qSort: one takes just a container
> reference, one takes start & end iterators and finally one takes start &
> end iterators and a compare function/function object.
Oh, ok. You are right. Sorry. Missed this.

-- 
 [ signature omitted ] 

Message 7 in thread

----- Original Message ----- 
From: "Pavel Antokolsky aka Zigmar" <zigmar@xxxxxxxxx>
To: <qt-interest@xxxxxxxxxxxxx>
Sent: Sunday, February 18, 2007 6:23 PM
Subject: Re: Basic QStringList sort question


> The code using it would look something like this (don't take it
> literraly - I didn't compile it):
>
> template<typename T>
> struct LengthComp
> {
> bool operator()(const T& first,const T& second) {
> return first.length() < seond.length();
> } };
>
> QStringList list;
> //...
>
> std::sort(list.begin(), list.end(), LengthComp<QString>());
>

Thanks Zigmar and everyone who replied.

I wasnât able to manipulate this piece of code to get it to compile without
errors but it is along the lines of what I had in mind.

Although, no doubt, not as elegant as Zigmarâs solution I did continue with
my efforts to code a bubble sort method which does work.

Regards,
Phil.

--
 [ signature omitted ] 

Message 8 in thread

Phil wrote:

> Thank you for reading this.
> 
> I need to sort the members of a QStringList in QString length order
> (longest words first). The documentation mentions using QMap <int>
> <QString> for nonstandard sorts but I can't see how this is done. Can
> someone point me in the right direction?
> 
> I started to code a bubble sort method but I'd like to see how Qt handles
> unusual sorts.

No need to create your own bubble sort.
http://doc.trolltech.com/4.2/qtalgorithms.html#qSort-2 should be what you
need. Just supply a function that compares the lengths of the strings.

HTH,

André

-- 
 [ signature omitted ]