Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++ : sorting a vector

Credits go to Arthur Kalliokoski, ImLeftFooted, and Stas B. for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
C++ : sorting a vector
Trent Gamblin
Member #261
April 2000
avatar

See, you don't admit you're wrong because you WERE and ARE wrong. What I showed in that code is how to produce a stable sort using std::sort. Geeze man, get over yourself.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Stas B. said:

I'm not mad at you or anyone.

Quote:

you need three spoons of sugar

Quote:

I couldn't care less

Quote:

I will keep calling you on your bullsh*t

You could have fooled me.

They all watch too much MSNBC... they get ideas.

Trent Gamblin
Member #261
April 2000
avatar

The only thing I did wrong in this thread is fail to explain that you don't always want or need a stable sort. That was an accidental omission. Feel free to point out any other "nonsensical bullshit".

Stas B.
Member #9,615
March 2008

See, you don't admit you're wrong because you WERE and ARE wrong. What I showed in that code is how to produce a stable sort using std::sort. Geeze man, get over yourself.

Are you honestly an idiot?
Your callback gives incorrect result both for sort and stable_sort.
See updated previous post for a demonstration.

You could have fooled me.

That's my default manner of speaking. Shame on me. :P
I don't outright flame people with personal attacks and I do try to help and answer people's questions, so bear with me.

Trent Gamblin
Member #261
April 2000
avatar

<facepalm>

1) The sort you used first was completely different from mine. Don't compare apples and oranges. YOUR results are wrong. It's supposed to sort them as integers.

2) Try putting MINE first, not after you've fucked up the arrays. It WORKS. Numb nuts.

Audric
Member #907
January 2001

For what it's worth, I find the documentation of STL's sort() very ambiguous on how the compare function should behave.
Libc's qsort() seems more sane to me as it specifies that the comparison functions should handle explicitely the 'equal' case. Most comparison functions I've written for qsort were simply return obj2->y - obj1->y;

Dizzy Egg
Member #10,824
March 2009
avatar

Finally an exciting thread! Come on Trent destroy him DESTROY!!!! ;D

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Trent Gamblin
Member #261
April 2000
avatar

I think I just did that.

EDIT: And I appologize for being a prick. I should have corrected you in a more calm manner :P... got the rage in me, it's a sickness.

axilmar
Member #1,204
April 2001

The function std::sort does not guarantee the preservation of order of equivalents; std::stable_sort does have that guarantee.

Stas B.
Member #9,615
March 2008

Audric said:

For what it's worth, I find the documentation of STL's sort() very ambiguous on how the compare function should behave.

That's actually true. Thankfully, it comes with an example. But to be honest, that's not even the point. The point was that the stability depends on the algorithm, not on the comparison function. Granted, it won't work if you don't provide it with the callback it expects, but that's obvious and besides the point.

1) The sort you used first was completely different from mine. Don't compare apples and oranges. YOUR results are wrong. It's supposed to sort them as integers.

2) Try putting MINE first, not after you've fucked up the arrays. It WORKS. Numb nuts.

I honestly don't understand what you're talking about. I hope you're just trolling... Correct me if I'm wrong, but you said that this callback:

bool compare_foos(Foo *a, Foo *b) {
   return a->bar <= b->bar;
}

Can be used with std::sort to achieve stable sorting.
My example demonstrates that it doesn't work with neither std::sort nor std::stable_sort. There's nothing wrong with my array. I copy it into a vector then sort the vector by using the integer part as the key. The fractional part is used to differentiate between the numbers to see if they end up in the original relative order. Clearly, they don't. Feel free to hack up your own example and post it here so we could all observe how it also fails.

Come on, guys. You're better than that. Somebody take a serious look at it and explain to him why he's wrong. ???

[EDIT]

To be fair, this example isn't even mine. It's from the STL documentation that demonstrates stable sorting. I just replaced their callback with yours.

Matthew Leverton
Supreme Loser
January 1999
avatar

The std sort is not stable when two elements are equal. How is that up for discussion?

Trent Gamblin
Member #261
April 2000
avatar

Ok, I'm wrong. I had my definitions wrong. By 'stable sort' I actually meant a sort that will return the same results every time. Hell, I may even be wrong about that, but I remember having different results returned when using < and not <= when implementing A* one time. So it's not a stable sort, and I'm not even sure that < will return different results at any time. I was sure that was the case though. Anyway, appologies for posting incorrect information.

Stas B.
Member #9,615
March 2008

Ok, I'm wrong. I had my definitions wrong. By 'stable sort' I actually meant a sort that will return the same results every time.

Oh. In that case, I'm sorry, Trent. I didn't realize we were actually talking about completely different things!

I'll just leave this here:

Trent Gamblin:
Stas B. said:
As far as I know, "stable" simply means that elements with equal keys keep their relative order after the sort.

Yeah, exactly, and that's exactly what the '=' does. Think about it.

YOUR results are wrong. It's supposed to sort them as integers.

2) Try putting MINE first, not after you've fucked up the arrays. It WORKS. Numb nuts.

Implying my example gave you different results every time?
It's obvious that the only reason for your apology is because I mentioned that's the proper way to behave in my previous posts. Your disgusting dishonesty still shows through, though. No. I'm not mad. I'm just calling you on your bullshit again.

Trent Gamblin
Member #261
April 2000
avatar

Ok dude, think what you want. I don't really care. If you can't accept an appology then you don't deserve one.

Stas B.
Member #9,615
March 2008

Ok dude, think what you want. I don't really care. If you can't accept an appology then you don't deserve one.

Oh, come on. Nobody can be that thick. You think I need your apologies? Seriously? It's just about being a decent person. How decent is it when you're outright lying in your apology? It's not about what I think. It's about what anybody who reads your posts thinks. Maybe I sound like a jerk, but at least I'm honest.

Trent Gamblin
Member #261
April 2000
avatar

I was being completely honest. I think you just have a hard time seeing any merit in anyone but yourself. You win on technical terms, but I have no doubt that I walk away the better person.

Stas B.
Member #9,615
March 2008

I was being completely honest. I think you just have a hard time seeing any merit in anyone but yourself. You win on technical terms, but I have no doubt that I walk away the better person.

You started flaming me with personal attacks as a response to my posts that talked exclusively about the technical matter at hand and not about you. After realizing you were wrong, you wrote a phoney apology where you lie about mixing up the definitions, as evident by the fact that some of your earlier posts make no sense in this new context and the fact that you've confirmed that we are in fact talking about the same thing. If you think you walk away the better person, I feel sorry for you.

Trent Gamblin
Member #261
April 2000
avatar

I was right about one thing: asshat. And that's not a compliment even though you wear it like a badge of pride. Don't feel sorry for me, my shit has been sorted for a long time now. Good luck with yours.

Dizzy Egg
Member #10,824
March 2009
avatar

Your imlplying that Trent is thick? Oh dear oh dear, a LOT to learn then Stas B.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

GibbSticks
Member #14,193
April 2012
avatar

Another thread completely destroyed because of petty arguments, sigh

:'(:-X::):-/

C++, C# developer

Hitler was a fan of Chaplin. Chaplin was therefore responsible for the murder of millions of lives.

bamccaig
Member #7,536
July 2006
avatar

I'm a little confused about this... It sounds like stable_sort is designed to work the way Trent's revision is without explicitly coding the comparer that way. So wouldn't the correct way to achieve a "stable sort" be using stable_sort instead of altering the behavior of your comparer?

???

Stas B.
Member #9,615
March 2008

bamccaig said:

So wouldn't the correct way to achieve a "stable sort" be using stable_sort instead of altering the behavior of your comparer?

Yes. The only way if you want to use the STL sort functions.

bamccaig
Member #7,536
July 2006
avatar

Trent Gamblin
Member #261
April 2000
avatar

bamccaig said:

@Stas B.: For what it's worth I don't think that you were being an asshat in this thread either...

Predictable.

Let's rewind and see who threw the first punch...

Stas B. said:

so your point is entirely moot

Stas B.
Member #9,615
March 2008

Let's rewind and see who threw the first punch...

Stas B. said:
so your point is entirely moot

But it was. :-/
I don't see why you'd find that so offensive. It's not even a personal attack. I just said your point was moot. I even bothered to explain why. :P

 1   2   3 


Go to: