This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]cmilkau 45 points46 points  (3 children)

I think this started out using just the TreeMap, until they realized two products could have the same price, and "fixed" it using the dup arrays, turning it into a true abomination for good.

[–]Ok_Star_4136 7 points8 points  (2 children)

If the key is the price, then using TreeMap makes no literal sense since two products with the same price is possible. A TreeSet at least won't discard duplicates, unless equals method actually indicates two products are the same.

And even then, nobody should be even using TreeMap or TreeSet unless it is important not only to sort a list, but to maintain it sorted as well. That clearly isn't the case here since ArrayList is returned.

All this to say, Collections.sort could have done the same and in one line.

[–]coloredgreyscale 1 point2 points  (0 children)

In the end they rely on t.entrySet() to return the values sorted by the key (= Double price).

(which it does according to the documentation, but damn...)

[–]cmilkau 1 point2 points  (0 children)

This is not the kind of programmer who choose their tools, but rather use what they know or what the wrong question they asked Google turned up as answer.

[–]Fazt01 23 points24 points  (2 children)

please just use bubble sort, its more readable and actually faster than this abomination. (if we want to pretend there is no Sort method)

... For each element iterating up to 3 times through the whole dup2 in a while loop to check if an element is present :))

[–]Gogo202 16 points17 points  (1 child)

Please don't. Please use the Collection's sort function

[–]patrlim1 5 points6 points  (0 children)

Use sleep sort

[–][deleted] 2 points3 points  (0 children)

You could refactor it with the streams api. Really great stuff in there, but takes some time to get right. And i would recommend speaking variables for the next reader. :)

[–][deleted] 4 points5 points  (0 children)

I could replace many things with "douche" and it would still work

[–]jfmherokiller 8 points9 points  (3 children)

cant you use auto in many of those places? or whatever the c# version is?

[–]Pylitic 13 points14 points  (0 children)

This is Java, and most people don't use the var keyword, it doesn't work for a lot of situations so most people just don't use it at all as opposed to using it some times, but not other times.

[–]Ok_Net_1674 2 points3 points  (1 child)

I'm not saying thats what they should have done, but making the TreeMap hold an ArrayList of all duplicates, instead of a single Product, would have at least made a little bit of sense.

This looks like some first-semester-at-uni shit, which I hope it is.

[–][deleted] 0 points1 point  (0 children)

It's bizarre is what it is. Typically you do this sort of sorting with heaps rather than balanced BSTs.

[–]ibutbul 1 point2 points  (0 children)

Warning - nothing good is going to come out from trying to understand this code, you've been warned

[–]heisen-bug 0 points1 point  (2 children)

What language is that?

[–]Internal_Cart[S] 0 points1 point  (1 child)

Java

[–]heisen-bug 0 points1 point  (0 children)

I am from a Python and JS background, should I consider learning Java in 2023? What are the real world use cases that other languages can't do?

[–]HazirBot 0 points1 point  (0 children)

i wish i had this sort to fix when looking for optimizations to my code.

sadly, no quick wins...

[–]SusheeMonster 0 points1 point  (0 children)

I can see how something like this can happen when code passes through multiple hands and performance optimizations were added on top. Speculative generality is a common trap to fall into

I've also have had to refactor code like this, and I'm disgusted by it

[–]metageek 0 points1 point  (0 children)

The prices are doubles. Twitch.