# Thread: sorting an array of doubles [solved]

1. Novice
Join Date
May 2012
Posts
57
Thanks
11
Thanked 2 Times in 2 Posts
Qt products
Platforms

## sorting an array of doubles [solved]

Hi,

I was asking about sorting doubles but i seem to have found the answer. Thanks.

Qt Code:
`//-------------------------------------------------------------------------------void sortdoubles(double *d, int num){QList<double> tmp;for (int i = 0; i < num; i++)  tmp << d[i];qSort(tmp);for (int i = 0; i < num; i++)  d[i] = tmp.at(i);}`
To copy to clipboard, switch view to plain text mode
Last edited by Cremers; 24th September 2018 at 16:04.

2. ## Re: sorting an array of doubles [solved]

qSort() is obsolete and should not be used in new code. Besides, you can do the same thing with one line of code from the Standard Template Library (STL):

Qt Code:
`std::sort( d, d + num );`
To copy to clipboard, switch view to plain text mode

3. Novice
Join Date
May 2012
Posts
57
Thanks
11
Thanked 2 Times in 2 Posts
Qt products
Platforms

## Re: sorting an array of doubles [solved]

Thanks. But if i want to know the sorting order and need to sort a pair, what do i do with std::sort()?
I am using QPair for that now:
Qt Code:
`QList<QPair<double, int>> tmp;for (int i = 0; i < num; i++)  tmp.append(qMakePair(p->x, p->n));qSort(tmp);for (int i = 0; i < num; i++)  {  p->x = tmp.at(i).first;  p->n = tmp.at(i).second;  }`
To copy to clipboard, switch view to plain text mode

Can I sort a structure like typedef struct { double x; int n; } mysortstruct; ?

4. Intermediate user
Join Date
Jul 2012
Posts
233
Thanks
21
Thanked 14 Times in 13 Posts
Qt products
Platforms

## Re: sorting an array of doubles [solved]

Yes, you need to have a coparison operator for your struct or pass a lambda to the std::sort. See here: https://en.cppreference.com/w/cpp/algorithm/sort

5. ## The following user says thank you to tuli for this useful post:

Cremers (29th September 2018)

6. ## Re: sorting an array of doubles [solved]

Using lambdas is a more advanced technique than simply defining a comparison function. For example, using a comparison function:

Qt Code:
`// UNTESTED CODE typedef struct{    double a;    int b;} MyStruct; // Comparison functionbool myStructLess( const MyStruct & lhs, const MyStruct & rhs ) // "lhs" = "left-hand side"{    if ( lhs.a < rhs.a )        return true;    else if ( lhs.a > rhs.a )        return false;    else        return lhs.b < rhs.b;} std::vector< MyStruct > myStructVector; std::sort( myStructVector.begin(), myStructVector.end(), myStructLess() );`
To copy to clipboard, switch view to plain text mode

But if you understand lambdas or functors, then use whatever method works best for you.

7. ## The following user says thank you to d_stranz for this useful post:

Cremers (29th September 2018)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.