Results 1 to 20 of 20

Thread: QStringList Sorting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList Sorting

    Quote Originally Posted by Lykurg View Post
    Ok, I see, too quick
    But with "abc-2.00234" and "abc-2.12352" n1 == n2 == 2?
    No! "abc-2." would fall into the common prefix (while s1.at(i) == s2.at(i)). Thus: n1="00234", n2="12352"

    Quote Originally Posted by Lykurg View Post
    Anyway, may it not be better/quicker first to determinate the number on the end of the string, and then chop the x characters (x = length of the determinated number)? Of course it depends of the string list you compare. Many different prefixes exterminating from the front would be faster...
    Well the last number could be equal. I think the approach to find the biggest common prefix and then look for the difference is the way to go. Thats how a natural/human sort does work anyway.

    Quote Originally Posted by Lykurg View Post
    Also interesting would be a speed comparison with a member reg expression...
    There is no doubt in my mind, that the explicit while loop will be faster. Even when you use a member RegExp, so that its mask-parsing is only done once.. the reg-exp-fitting code is much more general and thus carries a lot of overhead, compared to this specialized solution..

    The code would perhaps be more readable.. But I'm not so sure :->

    Greetings!

    Joh

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList Sorting

    Thinking about it again.. my natural sort doesn't work for "a2002, a20001". Here n1 would be ="2" and n2="01". So n2.toInt() < n1.toInt().. which is wrong, when you look at the whole number..

    So from the first different character we also need to look backward and add all adjacent numbers:

    Qt Code:
    1. bool compareNames(const QString& s1,const QString& s2)
    2. {
    3. // ignore common prefix..
    4. int i = 0;
    5. while ((i < s1.length()) && (i < s2.length()) && (s1.at(i).toLower() == s2.at(i).toLower())) ++i;
    6. ++i;
    7. // something left to compare?
    8. if ((i < s1.length()) && (i < s2.length()))
    9. {
    10. // get number prefix from position i - doesnt matter from which string
    11. int k = i-1;QString n = "";
    12. while ((k >= 0) && (s1.at(k).isNumber())) {n = s1.at(k)+n;--k;}
    13.  
    14. // get relevant/signficant number string for s1
    15. k = i;QString n1 = "";
    16. while ((k < s1.length()) && (s1.at(k).isNumber()) {n1 += s1.at(k);++k;}
    17.  
    18. // get relevant/signficant number string for s2
    19. k = i;QString n2 = "";
    20. while ((k < s2.length()) && (s2.at(k).isNumber()) {n2 += s2.at(k);++k;}
    21.  
    22. // got two numbers to compare?
    23. if (!n1.isEmpty() && !n2.isEmpty())
    24. {
    25. return (n+n1).toInt() < (n+n2).toInt();
    26. }
    27. else {
    28. // not a number has to win over a number.. number could have ended earlier... same prefix..
    29. if (!n1.isEmpty()) return false;
    30. if (!n2.isEmpty()) return true;
    31. return s1.at(i) < s2.at(i);
    32. }
    33. }
    34. else {
    35. // shortest string wins
    36. return s1.length() < s2.length();
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStringList Sorting

    I want to do the same thing sort of a class QStrings as integers. But I don't know where to put the functinon compareNames. As a class function? Or should I extend the qSort class?

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringList Sorting

    qSort is not a class but function.
    make your function compareNames as a global function or static class method or a struct with operator() (and pass object of that struct).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStringList Sorting

    I'd suggest first to determine the name sheme before throwing solutions at each other and then just use the one that fits the purpose....

  6. #6
    Join Date
    Oct 2008
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStringList Sorting

    For an implementation of Natural-Order Sorting (Case sensitive or non) you can have a look at the GPL code by Tobias Koenig <tokoe@kde.org> in these links: [Header - qnatsort.h] [Source - qnatsort.cpp]

    An implementation using qSort() is explained in the comments - you simply have to pass the included function.

Similar Threads

  1. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  2. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  3. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  4. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21: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
  •  
Qt is a trademark of The Qt Company.