Results 1 to 20 of 27

Thread: Sorting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Posts
    94
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Post Sorting

    I have value like

    abc1
    abc2
    abc10
    abc100
    bcd5
    abcd1
    abcd2
    abc3
    zbc1
    xyz1

    how can i sort it so that both text value and numeric value will be sorted. So that i will get the result like:

    abc1
    abc2
    abc3
    abc10
    abc100
    abcd1
    abcd2
    bcd5
    xyz1
    zbc1

    I.e. i want both alpha value and numeric value will be sorted.

  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: Sorting

    These situations are not a problem as a simple sort will do this. This becomes a problem:

    abc13
    abc103

    Do you want it sorted this way or the other way (103 first, then 13)?

    Anyway, use qSort() and provide a custom implementation of the LessThan functor
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Posts
    94
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting

    I am using Qsort , but how can i sort, which should be sorted in both alphabetically and numeric value?
    Please send me the code.

  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: Sorting

    I am using Qsort , but how can i sort, which should be sorted in both alphabetically and numeric value?
    Read again what Wysota posted:
    use qSort() and provide a custom implementation of the LessThan functor
    Run QtAssistant and read about qSort and QString to get know how to extract numeric part of your string. Probably it could be done easy with QRegExp. Then implement your own LessThan functor.
    Please send me the code.
    Please send me fresh donuts.
    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
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting

    Question already answered in full length here:

    http://www.qtcentre.org/threads/2141...930#post130930

    Johannes

  6. #6
    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: Sorting

    Quote Originally Posted by faldżip View Post
    Please send me fresh donuts.
    LOL! Made my day!

    You are right. That "give me" attitude is hardly the nicest way of asking for help.

    But who knows? Maybe it's just a language barrier?

    Johannes

  7. #7
    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: Sorting

    Quote Originally Posted by JohannesMunk View Post
    You are right. That "give me" attitude is hardly the nicest way of asking for help.

    But who knows? Maybe it's just a language barrier?
    Maybe but maybe not :P
    If it is just a language barrier then I hope my answer would encourage to find better words than "give me" next time.
    But I think that in many cases it is not only language. Half of the post can be answered by opening QtAssistant, finding required solution and rewriting it in QtCentre forum, sometimes with slightly changed example, just because the guy who started thread don't want to spend 10minutes reading documentation. And with "give me the code" attitude you don't learn anything. I know that because when I was starting to learn programming I wanted to have working code fast and was rewriting, copy-pasting code and then you have to do it every time because you have no idea how your code is working and why it is not working. And I learned programming on my studies but without doing additional exercises (besides those I had to do) at home I would know nothing. And Qt has a really good documentation and it has nice tool out of the box (QtAssistant) to allow you easily get anything you want about Qt classes. Assistant is running almost all the time on my computer so I can check the way I should use the Qt classes at any moment. It's icon is placed next to the web browser icon on my quick launch bar :] And on browser's bookmarks bar there is a cppreference.com button so I can check basic C/C++ functions, classes because it takes much less time then making new thread on forum and waiting for answer...
    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.

  8. #8
    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: Sorting

    I agree with you, that copy pasting code without even reading it, doesn't help. But that's up to the reader.

    I generally agree that, if someone asks for a fish its usually better to teach him fishing. But giving him the fish that you fished together, is acceptable too, I think :->

    On the other hand the question here (natural sorting of strings, see the other posts of OP) can not be answered with Qt Assistant. And without knowing the right term not with google either.

    What disturbs me, though, is that because some people exploit this kind of forum as the chance to get everything without any hasle at all and without even saying thanks afterwards, the tone of the answers gets quite rough, sometimes. And that's not a healthy development, I think. Neither for the stress level of regular posters, nor for the forums atmosphere.

    Johannes

  9. #9
    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: Sorting

    Quote Originally Posted by JohannesMunk View Post
    On the other hand the question here (natural sorting of strings, see the other posts of OP) can not be answered with Qt Assistant. And without knowing the right term not with google either.
    But typing "QString sorting" in search line edit of this forum and pressing enter will give the topic which you linked few posts above, where the solution was discussed and already done. Using compareNames() function in qSort() requires rather C++
    only knowledge. So I almost agree with your one point - no need to use QtAssistant to get the solution, because it is already here.
    Another thing is that what people cannot notice easily is that Qt is a C++ library, not programming language, so you are not programming in Qt, you are programming in C++ with Qt. So the simple implication is that you have to learn C++ to use Qt. And you don't have to know everything about C++, but there is some subset of C++ features which are commonly used and covered by almost every book and tutorial.
    And this is the case here also... The solution is given, now you have to use it regarding C++ syntax (It is even mention how in above linked thread).
    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.

  10. #10
    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: Sorting

    Quote Originally Posted by faldżip View Post
    Half of the post can be answered by opening QtAssistant, finding required solution and rewriting it in QtCentre forum
    Half? I'd say "about 90%". The rest just requires some experience.

    Quote Originally Posted by JohannesMunk View Post
    I agree with you, that copy pasting code without even reading it, doesn't help. But that's up to the reader.
    That would be true if he had to pay some money for the solution to the person who provided it. But if answers are free then why bother looking for them yourself if there are people who will do it for you? So I'd say it's really up to the person providing the solution. Next time he/she simply won't bother doing that at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    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: Sorting

    Quote Originally Posted by wysota View Post
    That would be true if he had to pay some money for the solution to the person who provided it. But if answers are free then why bother looking for them yourself if there are people who will do it for you?
    What would money change? Would it take away the feeling of being exploited? I think, that can only be cured by an honest "thank you". And some people are kind enough to say it. We should have a law for it, though :->

    Quote Originally Posted by wysota View Post
    So I'd say it's really up to the person providing the solution. Next time he/she simply won't bother doing that at all.
    What somebody does with something you helped him with is entirely his thing. I think that's something fundamental to true help. Think of somebody saving another one's life. He owes him a big thank you. But the saver doesn't own the saved person's life afterwards. Otherwise its not help, but some kind of extortion in disguise.

    With helping the situation is always a little pre-loaded.. Because as helper you do expect some kind of gratitude. If that gratitude fails your expectations or is replaced by bold demands, trouble is underway. I don't have a solution for that. I just tend to avoid those people afterwards. And try to keep my hopes up, that the next person will not fail me.

    Good night!

    Johannes

  12. #12
    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: Sorting

    Quote Originally Posted by JohannesMunk View Post
    What would money change? Would it take away the feeling of being exploited?
    It's the other way round. You'd ask questions more carefully and try doing something yourself first if you had to pay (even a small fee) for each answer you get.

    What somebody does with something you helped him with is entirely his thing.
    Oh, no argument here. Of course provided he doesn't violate any licence.

    I think that's something fundamental to true help. Think of somebody saving another one's life. He owes him a big thank you. But the saver doesn't own the saved person's life afterwards. Otherwise its not help, but some kind of extortion in disguise.
    Would you jump from a cliff if you knew someone would save you? Would you jump if you didn't know if someone would be there to save you?

    Of course you can also look at things from the other person's perspective: If someone would be jumping off the cliff all the time, wouldn't you grow tired of saving him constantly and telling him not to do it again?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Sorting

    Quote Originally Posted by faldżip View Post
    But typing "QString sorting" in search line edit of this forum and pressing enter will give the topic [...]
    Speaking of the search line edit (as i see we are not speaking of qstring sort anymore ), do you now how to combine terms ? I tried

    Qt Code:
    1. QString + sort
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. QString AND sort
    To copy to clipboard, switch view to plain text mode 

    but none of these gave me the actual answer, that is to say a thread title containing both "QString" and "sort" words. Is there an explanation somewhere ? (didn't find it)

  14. #14
    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: Sorting

    I would type just:
    QString sort
    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.

  15. #15
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Sorting

    Quote Originally Posted by faldżip View Post
    I would type just QString sort
    and it would not give you post with these 2 words, but with one of them. At least it doesn't work for me

    @wysota : speaking of QString sorting was just an example. My real question is actually how to combine terms in search line.

  16. #16
    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: Sorting

    Have you seen the "Similar threads" list on the bottom of this page? It's worth to start your search there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Sorting a qLinkedList
    By pwaldron in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2010, 21:59
  2. QTableWidget sorting
    By losiem in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2008, 20:20
  3. QListView sorting
    By fear in forum Qt Programming
    Replies: 14
    Last Post: 21st August 2007, 13:08
  4. Sorting a QVector
    By jiveaxe in forum Qt Programming
    Replies: 21
    Last Post: 11th August 2007, 19:24
  5. Sorting in QTreeWidget
    By adhit in forum Qt Programming
    Replies: 15
    Last Post: 8th May 2007, 12:49

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.