Results 1 to 5 of 5

Thread: sorting an array of doubles [solved]

  1. #1
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default sorting an array of doubles [solved]

    Hi,

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

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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,016
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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:
    1. std::sort( d, d + num );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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:
    1. QList<QPair<double, int>> tmp;
    2. for (int i = 0; i < num; i++)
    3. tmp.append(qMakePair(p->x, p->n));
    4. qSort(tmp);
    5. for (int i = 0; i < num; i++)
    6. {
    7. p->x = tmp.at(i).first;
    8. p->n = tmp.at(i).second;
    9. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jul 2012
    Posts
    233
    Thanks
    21
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,016
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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:
    1. // UNTESTED CODE
    2.  
    3. typedef struct
    4. {
    5. double a;
    6. int b;
    7. } MyStruct;
    8.  
    9. // Comparison function
    10. bool myStructLess( const MyStruct & lhs, const MyStruct & rhs ) // "lhs" = "left-hand side"
    11. {
    12. if ( lhs.a < rhs.a )
    13. return true;
    14. else if ( lhs.a > rhs.a )
    15. return false;
    16. else
    17. return lhs.b < rhs.b;
    18. }
    19.  
    20. std::vector< MyStruct > myStructVector;
    21.  
    22. 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Cremers (29th September 2018)

Similar Threads

  1. How to parse nested JSON Array and Object inside array
    By Radhika in forum General Programming
    Replies: 5
    Last Post: 17th December 2015, 08:42
  2. tableview with doubles
    By Arend in forum Newbie
    Replies: 2
    Last Post: 26th November 2012, 06:43
  3. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  4. Replies: 2
    Last Post: 12th November 2010, 14:42
  5. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24

Bookmarks

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.