- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- Three Little Nyms
- Tangled Up In Blue
- 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
First!
Admin
Now that's a lot of code for doing really very little...
Admin
Is there any possible way of doing that worse? Just in performance with all the loop interations it must be a joy. And debugging that monstrosity would be horrifying.
Admin
Sorry. I was just so frickin excited to see there were no posts yet!
Admin
Why didnt he just do a single loop with a custom comparison function?
Admin
I fully endorse the genocide of people like yourself that get excited by being the first to post, or think that such an accomplishment is worthy of any kind of kudos or acknowledgement.
Admin
an unmaintainable, barley-compilable heap of codefiles
I'd probably want plenty of whisky if I was working with this code, yes. Let's not just do a bubble sort but let's do it four times...
Admin
I was thinking a randomized algorithm would work - something that provably gets your items close to sorted within some bound, and then you run it lots and lots of times :-)
Admin
Ah! I see the WTF - he's using a Vector...
Admin
You know what they say: When one bubblesort isn't enough, use four!
Admin
I'm sure he just read about a Radix sort somewhere, and implemented it in the worst possible way ever.
It sort of looks like one... if you squint, anyway.
Admin
Why not just use a SortedSet?
[ducks]
Admin
I'm guessing the WTF is that he's using Java?
Admin
Ah, Now I see...it's not thread safe
Admin
May I join this genocide?
Admin
At least in Java 5 code like this is no longer needed (just the snippet here, not whole wtf). I've had to do it several times and always found it a little annoying.
Now (assuming filesVector is defined as Vector<String>), all that can be replaced with-
String[] filesArray = filesVector.toArray();
or something like that, I forget exact syntax
Admin
Of course. Vector fileVector. This is obviously a vector of files... no wait. Strings.
/not like naming it filenameVector would make the function justifyable..
Admin
Yeah well Collections.Sort() might contain bugs so he goes and implements every critical operation himself... that can't be wrong :P
Admin
ugh. Meant to quote Ytram. The "edit time" timed out.
I've cleaned up behind so many "programmers" afraid or ignorant of recursion. And so many DIY-ers. This is a bad combonation of the two.
Admin
I also endorse the genocide of the people who add another ten stupid posts saying "I hate people who post 'first'". And the people who make posts bitching about people who bitch about people who make 'first' posts. And yes, I realize I am embracing my own genocide. If that is how it must be then that is how it must be. I will be remembered as a martyr!
Admin
Yet another Java programmer who doesn't know what he is talking about. Vector#toArray() is there since ancient Java 1.2. Particular the version were you supply your own array as an argument works like a charm since ages when you need to get a special type of array. </string>
Admin
combonation - such a booutiful woord
Admin
I'd be interested to know how many man hours went into this. I'm guessing probably not too many, since it looks like the result of 3 copy-n-pastes.
Admin
(OT) At least he said he was sorry.... besides, I don't think genocide is the answer. We need people like him.
Admin
<font size="2">Wow, just... wow. The SpecialOlympicist really couldn't have done much worse with that, could he?
</font>
Admin
All of the people who would remember you as a martyr would be subject to the genocide to. Sorry.
Admin
<font size="1" style="font-family: verdana;">
<font size="2">Well, he did combone several WTFs in creating this masterpiece. It's a perfectly cromulent word.
</font></font>
Admin
Second! Doh! not quite...oh well...maybe tomorrow
Admin
Cool, I didn't realize that. I had tried casting the result to a special array (String[])vector.toArray() ... which obviously doesn't work since Object[] can't be casted to String[]. I guess I stopped there since a loop was simple and I didn't catch that the one with the parameter would correctly handle a String[] argument.
At least I know I'm not the only one that doesn't know this.. I've seen a loop like this in other people's code many times, but I've never seen the toArray(Object[]) method used.
Admin
Don't reply, you're only encouraging it!
Admin
The toArray(Object[]) method is available in everything that implements java.util.Collection.
The first time I tried to use this method, I struggled to get the syntax correct. Here is an example to keep handy...
import java.util.Collection;
import java.util.Vector;
public class ToArrayDemo {
public static void main(String[] args) {
Collection c = new Vector();
c.add("Foo");
c.add("Bar");
c.add("Baz");
String[] a = (String[]) c.toArray(new String[]{});
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
Admin
It would be fun to have whole contests made up of Rube Goldbergs like this where teams have to figure out what it actually does the quickest, and with the least amount of violence. And the winners get the author's personal e-mail address :D
Admin
I agree that it is far from intuitive to use this method as I originally struggled with it also. I came up with a slightly different take that works fine, and I think is a little bit clearer.
Collection c = new Vector();
c.add("Foo");
c.add("Bar");
c.add("Baz");
String[] a = new String[c.size()];
c.toArray(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
So, to bring it back to the WTF, this:
Could have easily been done like this, no loopy looping needed:
Admin
Heh, I see. Year has four characters to it, so how can this possibly work? And Month and Day and HH have 2 a piece. This calls for my patented decasort(), with 10 while loops. It's 150% better than quadrasort!
No wait, don't you have to bubblesort each bit separately? What's 80 in Latin?
No wait, is this Unicode?
--Rank
Admin
This all makes so much sense!!!! Brillant!!! I fully expect a new API to result from this. It should implement:
Name of sort File name pattern
========== ============
UnarySort - YYYY.ext
BinarySort - YYYYMM.ext (who cares if this name is already taken.)
TernarySort - YYYYMMDD.ext
QuadraSort - YYYYMMDDHH.ext
QuinarySort - YYYYMMDDHHMM.ext
SenarySort - YYYYMMDDHHMMSS.ext
SeptenarySort - YYYYMMDDHHMMSSMMM.ext
OctalSort - YYYYMMDDHHMMSSMMMNNN.ext
Bah!
Admin
And for records of arbitrary length, "ChampagneSort".
Admin
< SenarySort - YYYYMMDDHHMMSS.ext>
I would have called it:
SexySort - YYYYMMDDHHMMSS.ext
It has a nicer ring to it.
Thanks
Admin
Okay... there's quick-sort, and bubble-sort, and now!.... safety-sort. - 'Cause you can never be sure that items won't magically change during the sort. Hey- at least the bad dates were 'logged' ( for some reason ).
Admin
Personally, I prefer this:
String[] a = (String[]) c.toArray(new String[c.size]);
All one line without having to create an extraneous empty String array.
Admin
of course I meant:
String[] a = (String[]) c.toArray(new String[c.size()]);
Admin
I see/agree with the "all-on-one-line" argument,but where is my extraneous empty String array? I'm pretty sure that both way are:
1) Creating a string array with the exact capacity needed to fill it with the String objects in the collection
2) Fill up the String array
3) Keep a reference to the String array ... called "a" in this case.
The only difference is that I keep the reference from when I created the array, whereas you get the reference by casting the return object from the toArray method. Maybe we can chalk this up to programmer preference/style? Programming is an art form after all.
Admin
Atleast he is doing exception handling...
Admin
Well thank dog for that.
Admin
"What's your vector, Victor?"
Admin
dunno why, but I just thought of something..
For all the developers that talk about genocide of dumb developers, I wonder if any of you guys are the ones that are making these types of manslaughter to coding. It was definitely off topic, but yeah.
Admin
TankerJoe's two line method is slightly faster. It evaluates to...
35: aload_1
36: invokeinterface #8, 1; //InterfaceMethod java/util/Collection.size:()I
41: anewarray #9; //class String
44: astore_2
45: aload_1
46: aload_2
47: invokeinterface #10, 2; //InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
52: pop
While whoisfred's works out to...
35: aload_1
36: aload_1
37: invokeinterface #8, 1; //InterfaceMethod java/util/Collection.size:()I
42: anewarray #9; //class String
45: invokeinterface #10, 2; //InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
50: checkcast #11; //class String;"
53: astore_2
Admin
Just to join the fun.. I'm assuming this isn't in Java 5 since in 5.0 you don't need to cast if you've declared the collection a container of Strings.
Admin
I was thinking of writing something like this - keep trying random combinations until it was sorted. I wanted to make sure it was an algorithm, though (that is, finish in a finite amount of time), so I had to come up with a randomizer that wouldn't give me the same (dis)order twice, and would guarantee I would hit every permutation.
I didn't have time to mess with that, though. (It was for a programming competency test; the problem was: write your own sorting function. (Yes, this is quite simple; the test was meant to weed out such "programmers" as the ones who have unintentionally contributed to this site.))
I ended up writing a sort that tried every possibility until it was sorted. For example, if given DABC, it would do:
[list] []DABC []DACB []DBAC []DBCA []DCAB []DCBA []ADBC []ADCB []ABDC []ABCD [/list]
Alas, the testers didn't make a comment. (I wonder if they even read the code.)
P.S. First Post! (For me, at least.)
P.P.S. But I've been lurking here a while.
P.P.P.S. And yes, I do usually tend to ramble on and on and on ...
Admin
OK,
So does anyone want to take a stab at why he had to do the exact same sort 4 times?
He could've done it just once and accomplish the exact same thing so what is special about the number 4?
I could not come up with any theory about this.
Any ideas?
Admin
hmm... looks like each episode checks a specific date ( fragment ) for errors. looks like the programmer is mining for date-syntax-error trends?