Results 1 to 4 of 4

Thread: How to sort a Qlist AND get a sort key?

  1. #1
    Join Date
    May 2010
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to sort a Qlist AND get a sort key?

    Howdy,

    I have a several Qlists of equal length. Of those one contains a correlation value that I need to sort all of the Qlists on. I would like to get a sort key for the correlation Qlist that indicates its index in the unsorted correlation Qlist that can be used to access values in the other Qlist.

    If A is the unsorted correlation list and B is the sorted correlation list, the sort key IX should result in
    Qt Code:
    1. A[IX[i]] = B[i];
    To copy to clipboard, switch view to plain text mode 

    I am porting code from MATLAB to QT and this functionality is build directly in MATLAB's sort() function. The MATLAB documentation for sort() can be found here.

    This seems like it should be a routine thing. Does anybody have any suggestions?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort a Qlist AND get a sort key?

    I would define a struct that contains the elements that you want to link together. You can put those into a single QList. If you define the < (less than) operator correctly, then you should have your sorted list without having the need for a translation to the old location.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    May 2010
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to sort a Qlist AND get a sort key?

    That is true, but I guess I was a little misleading with my wording. I would prefer to not actually sort the lists for various reasons other than the single correlation list.

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort a Qlist AND get a sort key?

    Chances are someone else wrote some function that does that for you. Else you would have to write it yourself. I'm pretty sure qSort() doesn't do that for you.

    There's always the possibility of the struct I mentioned before, but only containing the thing you want to sort on and an integer. You copy the list, fill the integers with the original index and sort the new list based on your criteria. You can return just the list of integers if you prefer.

    Maybe it still doesn't suit your needs, but I'll give you a jump start on my idea.

    -- spoiler --
    Qt Code:
    1. struct SortItem
    2. {
    3. QString itemToSort;
    4. int origIndex;
    5.  
    6. bool operator<(const SortItem &other) const
    7. {
    8. return itemToSort < other.itemToSort;
    9. }
    10. };
    11.  
    12.  
    13. // ...
    14. QList<SortItem> items;
    15. // fill the list
    16. QList<SortItem> sortedItems = items;
    17. qSort(sortedItems);
    18. // run through sortedItems and get the origIndex to reference the item in the original lists.
    To copy to clipboard, switch view to plain text mode 
    Last edited by franz; 26th July 2010 at 18:50. Reason: updated contents
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. can't get right data after sort
    By wirasto in forum Qt Programming
    Replies: 5
    Last Post: 28th June 2009, 18:19
  2. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  3. How to sort QMap
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 10:11
  4. Sort datatable
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2006, 08:43
  5. When sort the lists?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 14th April 2006, 16:30

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.