Results 1 to 3 of 3

Thread: How to sort QMap

  1. #1
    Join Date
    Sep 2007
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to sort QMap

    I have a QMap<QString, QString> allSources in witch key is a "table_by_view" names, as vlues is "view_title".
    Example:
    "Activity_view", "Activiti";
    "pOrders_view","orders";
    "car_specification_view", "cars";

    And so on.

    But now I need to sord by order "view_title". A/b/s and so on..
    How can I do this, for get :
    "activity_view","activity";
    "car_specification_view","cars"
    "pOrders_view", "orders";

    Now i get only values as QStringList , sorting it and geting values in right order, but key in old order...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to sort QMap

    A map is an associative container, it stores its values sorted by keys to improve access speed to values. There is no way of sorting a map by value order. You have to use QList<QPair<x,y>> instead.

  3. #3
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to sort QMap

    or, if you need only sorted titles without associations to keys, use
    Qt Code:
    1. QStringList list = QMap::values();
    2. qSort(list.begin(), list.end());
    To copy to clipboard, switch view to plain text mode 

    U may get associated keys by calling QMap::key(const T & value );

Similar Threads

  1. How to sort the treeWidget item according to size and date
    By santosh.kumar in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 10:32
  2. How to sort a QComboBox in Qt4
    By guenthk in forum Qt Programming
    Replies: 7
    Last Post: 25th September 2006, 18:35
  3. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 12:12
  4. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08
  5. Is there a Pointer Based QMap or Similar
    By vasudhamirji in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 14:34

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.