- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Ridiculous! Throw it all away and replace it with a bubble sort, that's what I'd do.
Admin
At least use insertion or selection sort. That's still bad (for large lists) but a constant factor faster than bubble sort.
Admin
Lambda-think wasn't a thing back in the COM <> .NET early interop days.
The result of this sort is interesting, where customer ID 100 is less than customer ID 2.
Admin
Sure, but comparator classes and delegates absolutely were. And I was still doing interop all the way up into the 3.5 days, so lambdas still overlap.
(Yes, it sucked)
Admin
All you have to do is include the iComparable interface and implement CompareTo() and then you can sort the list of Customers. I guess even doing that would be too much learning/work.
Admin
I swear about 25% of the dev community stopped learning anything about algorithms or their language once they were half-assed familiar with arrays, for loops, and strings.
If you can't build it out of those 3 things, it must be impossible. Sheesh!
Admin
Zero sorts to give, huh?
Admin
I agree there's room for improvements. Here's my top 5 in arbitrary order and varying levels of seriousness:
Admin
I see no reason to assume ID is numeric.
Admin
Probably just a bit annoyed and upset.
https://en.wiktionary.org/wiki/out_of_sorts
Admin
This doesn't require Lambda-think to make it sane. Not saying that lambdas aren't the best choice, but there are non-lambda solutions that don't make you want to poke your eyes out.
Admin
Surely no programmer who wants to be productive likes nested loops dealing with two indices independently (like in Bubble-sort). That's why I prefer this algorithm (not named out of modesty): check if the list is in order; if not: randomly choose 2 neighbors, swap them and repeat. It uses only one index, only one loop, and is even adaptive to perfectly sorted inputs.
PS: if anybody dares to suggest there's a 2nd, hidden loop I will point out the fact that "repeat" is actually a "goto", so that doesn't count.
Admin
Everyone has also assumed that the ID is unique in the list. If not, Items will be lost after the sort (and others duplicated) since it only considers the first match.
Admin
You're absolutely right, and the main cause is that we've all been indoctrinated to use variable names that reflect the contents/meaning/properties/etc. of their values. Consequently, almost nobody realized that IDs might not be unique and that's a worrying thought. Perhaps it's time for a new dogma: I propose that from now on all variables, functions, procedures etc. are to be named x0, x1, x2, ... Just to not upset anybody I'll leave name spaces out of this (for now).
Admin
Reminds me of a function I wrote (also in VB.NET) to sort four arrays that contained related data (like, array 1 was a list of person IDs, array 2 was a list of the names of those people in the same order, and so on). So rather than replacing the four arrays with something sensible, like a single array of some sort of class that contains four properties, I went ahead and wrote a function to sort the arrays in place. I don't remember the details but I remember it took four generic type parameters, one for each array, and used tuples to couple the items together. Why did I do this? I honestly don't remember, my best guess is that refactoring all the code that generated the arrays would be too error prone. But at least it was faster than the old array sorting code. It was a bubble sort. I kid you not.
Admin
The .Clear() at the end followed immediately by an assignment is a real gem. He's overwriting the original List reference anyway, so why clear it. It won't make any difference to GC unless it's triggered between those two statements, because .Clear() just blanks the internal array without making it any smaller. I suppose it would have been worse if he'd then done an AddRange() instead (that's basically memcpy).
Admin
But if you don't name your spaces, how do you tell subroutines whose names are identical except for the name of the spaces embedded within them?
Disclaimer: this response was brought to you by a nightmare I had some years back, rather than any actual code I've seen in real life. I am delighted to say that I can't accurately depict the sort of attrocity I'm talking about, because we do not even have IDEs that can render that insanity.
Admin
Standard Linux style sort gives the same result.