PDA

View Full Version : Sorting



bismitapadhy
28th January 2010, 09:49
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.

wysota
28th January 2010, 10:18
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

bismitapadhy
28th January 2010, 10:36
I am using Qsort , but how can i sort, which should be sorted in both alphabetically and numeric value?
Please send me the code.

faldzip
28th January 2010, 11:29
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.

JohannesMunk
28th January 2010, 11:37
Question already answered in full length here:

http://www.qtcentre.org/threads/21411-QStringList-Sorting?p=130930#post130930

Johannes

JohannesMunk
28th January 2010, 11:56
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

faldzip
28th January 2010, 12:55
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 (http://www.cppreference.com/wiki/) 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...

JohannesMunk
28th January 2010, 13:25
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

faldzip
28th January 2010, 13:53
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).

wysota
28th January 2010, 21:19
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.


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.

JohannesMunk
28th January 2010, 21:55
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 :->


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

wysota
28th January 2010, 23:59
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?

JohannesMunk
29th January 2010, 00:35
Damn.. now I wrote for 15minutes.. and pressed the wrong button. Everything lost.. Happened to me twice today. Can't the "+Reply to Thread" Button be disabled, when using the Quick Reply box?

I'll answer again tomorrow!

Johannes

wysota
29th January 2010, 01:26
Happened to me too :) If you're using Firefox, install the "Lazarus" extension.

squidge
29th January 2010, 09:39
An easy solution is not to use the quick reply box :) I always click "Go advanced" to take care of this. I've been bitten before.

JohannesMunk
29th January 2010, 10:48
Thanks for sharing your button experiences :-> I have installed Lazarus. And for the longer replies I'll use the advanced mode from now on :-> But back to our discussion!


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.
I see your point! But that could also be solved by reputation. If each question/thread got a "laziness"-rating, where you could pursue your frustration when answering, and each profile would show an average laziness value of the threads started, maybe that could bring some lazy-posters down. As the thanks button boosts helping..


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?
Good imagery! I would not. Both times. I'm not suicidal :-> Speaking of saving a life was maybe a bit to drastic of me. Because I don't think that a lot of people around here, start there projects, planning to rely on external (free) support. It's maybe more like getting a little lost underway. And you can either stumble around on your own for some time, or you can ask the nice lady in the shop for directions.


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?
Well if the same person got lost all the time, and each time demanded that I drove him home, I would give up after some time, sure. That's whats happening here I think. But the problem is that there are so many people, getting lost a bit. And each one of them deserves a fresh start! Without being ranted at. And thats the problem with a bad atmosphere: Its quite undirected..

What do you think? Would a laziness value help?

Johannes

wysota
29th January 2010, 12:30
We already have a reputation scoring system here. You can click the star icon under each post and modify the author's reputation rating. Try it :)

JohannesMunk
29th January 2010, 12:41
Allright. But there is no public consequence. Where is the reputation rating visible for every one else?

wysota
29th January 2010, 12:53
But there is no public consequence.
There will be once people start using the system. For example we'll disable posting in any forums but the newbie forum for people with low reputation.


Where is the reputation rating visible for every one else?
The green rectangle under a user name and user title in each of the posts marks the reputation. Hovering over it will give a more elaborate description. You can compare your reputation, mine and that of franco.ammato - they will differ.

JohannesMunk
29th January 2010, 13:10
This is great! Why don't you make a sticky explaining all of this in detail? Because I looked for news of the site itself.. wanted to read about the new design, but couldn't find anything.

How are the comments on the post going to be used? Just as a note to the poster? Or visible for the public? How should they be phrased? What levels of reputation are there?

Johannes

pitonyak
29th January 2010, 17:26
There will be once people start using the system. For example we'll disable posting in any forums but the newbie forum for people with low reputation.

Interesting idea that may have prevented any of my replies to date because I am new and as such, have no reputation.

wysota
29th January 2010, 21:18
You do have enough reputation that wouldn't prevent you from posting elsewhere.

totem
29th January 2010, 23:16
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


QString + sort

and


QString AND sort

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)

faldzip
29th January 2010, 23:39
I would type just:


QString sort

wysota
30th January 2010, 02:11
Have you seen the "Similar threads" list on the bottom of this page? It's worth to start your search there.

totem
30th January 2010, 12:51
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.

squidge
30th January 2010, 17:59
How about:

http://www.google.co.uk/search?hl=en&source=hp&q=site%3Aqtcentre.org+%2Bqstring+%2Bsort&meta=&btnG=Google+Search

You don't have to use the search provided by the site to search the site :)