Results 1 to 6 of 6

Thread: Sort a QHash<QRgb, int> by value

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sort a QHash<QRgb, int> by value

    Hello,

    how can i sort a QHash by the int value?
    qSort doesn't work with QHash.

    thanks
    Niko

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sort a QHash<QRgb, int> by value

    Hashes have no order. You can create a list of pairs and sort it.

  3. The following user says thank you to jacek for this useful post:

    niko (11th September 2006)

  4. #3
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sort a QHash<QRgb, int> by value

    You can't sort QHash. From docs:
    When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
    If you want to have elements of QHash<QRgb, int> sorted, you could copy them to QMap<int, QRgb>. In that way you'll have them sorted by int.

    EDIT:
    Although Jacek gave better solution
    The Wheel weaves as the Wheel wills.

  5. #4
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sort a QHash<QRgb, int> by value

    thanks for the answers!

    Hashes have no order. You can create a list of pairs and sort it.
    you mean like this?
    Qt Code:
    1. class ColorCount {
    2. public:
    3. QRgb rgb;
    4. int count;
    5. operator <();
    6. };
    7. QList<ColorCount> colors;
    8. ...
    9. qSort(colors);
    To copy to clipboard, switch view to plain text mode 

    i thought about that too allready, but adding values to the list looks quite difficult and expensive to me, with QHash i could do:
    Qt Code:
    1. QHash<QRgb, int> colors;
    2. colors[QRgb(...)]++;
    To copy to clipboard, switch view to plain text mode 
    and with the list i have to search everytime through the whole list to find the right entry.

    ..although i could create a temporary QHash that just stores the position of the color in the QList... i guess the would speed it up again!


    thx
    niko

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sort a QHash<QRgb, int> by value

    Quote Originally Posted by niko
    although i could create a temporary QHash that just stores the position of the color in the QList
    Or create that list after you have finished colour counting.

  7. #6
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sort a QHash<QRgb, int> by value

    yes, thats easier;

    thx

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.