Results 1 to 4 of 4

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

Hybrid View

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

    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
  •  
Qt is a trademark of The Qt Company.