last day (14 days later) » 

3:02 PM
0
A: Sort an ArrayList that is based on a class?

DiciYou just need to create an order on your type Person by implementing the Comparable interface or an external Comparator. class Person implements Comparable<Person> { @Override public compareTo(Person that) { // this is a reasonible implementation, but you can have your own ...

 
So on the flip side, your answer contains more explanation but none of the code snippets compile.
 
I have no JDK on this computer, I will check the code when I am able to do so, unless you tell me where are the problems
 
It's less of a criticism of your answer and more of a criticism of your attitude towards Jean's answer. The last compiler error that I see is that you can't create an anonymous class with a diamond.
 
Well, I'll admit an answer with broken snippets is no ideal but I'm not on my personal PC. Nonetheless, the answer I criticized was a lazy answer, exploring only one aspect of the question. I believe my broken snippets with explanations are more informative than a correct one-liner. Best answer is probably Felk's until I fix mine, but we roughly have the same contents. Thank you though, I'm fixing it (still without a JDK)
@Radiodef Oh yeah, I wouldn't have found this one without a compiler. The Java compiler is so stupid when it comes to type inference... thanks
 
Well that one is necessary because an anonymous class is implicitly a declaration. So it's essentially the same as trying to do class Foo implements Comparator<> {...}. And I didn't try to compile it, I just saw the errors. ; ) Maybe Jean has an excuse for his short answer too.
 
3:02 PM
@Radiodef I don't have any excuse, the one-liner explain itself and if really OP doesn't understand it, there is hundreds of duplicates all over Stack Overflow. However, I appreciate that you tried to back me, thanks for that.
@Dici I'm really curious about your second snippet, why do you convert a list to a stream, then sort it, then re-convert to list ? Why not directly sort it like I did ? This is not what sorted is meant to be used.
Also, OP said he want to sort based on last name, not age.
Also, why not directly call compareToIgnoreCase instead of String.CASE_INSENSITIVE_ORDER.compare ?
 
@Jean-FrançoisSavard Collections.sort is less general purpose, it only applies to List whereas passing by a stream allows to do it with any kind of collection, and output any kind of collection (possibly not the same). Also, I prefer this functional style which returns a collection instead of mutating the collection passed as a parameter with a void return type. The last concern could be performance because of the collect operation, but depending on the type of the list, Collection.sort might also perform a copy if the list does not implement random access.
@Jean-FrançoisSavard The example of the age is to illustrate the fact that Comparator usage enables to write comparison on various criteria whereas implementing Comparable only provides a single definition for the ordering. That's typically the kind of things your "answer" lacks of
@Jean-FrançoisSavard because I forgot this method exists. Yes, it would be shorter with it, that's the only point you got right
 
" The example of the age is to illustrate the fact that Comparator usage enables to write comparison on various criteria whereas implementing Comparable only provides a single definition for the ordering."; Make non-sense, you will have to call different comparator.
"Collections.sort is less general purpose, it only applies to List". We already know it is a list. You might want to read Collections source-code for better point of view.
 
And what if it wasn't ? I'm not waiting the OP to ask the same question on a Set, I give him an answer as complete as possible which will hopefully help him in a wider scope than the original question. This is the difference between an answer and a lazy one-liner
@Jean-FrançoisSavard yeah, exactly... You can write n Comparators but implement Comparable only once. This difference is worth to be mentioned in a complete answer
 
Seems like your really proud of your so-complete answer ;)
 
Nope, this is a reasonible answer, that's all, just like Felk's. Yours is not, that's all I'm saying. Anyway... bye man
 
3:02 PM
@Dici That's probably why mine was the most upvoted one. Not even mentionning that Radiodef had to correct yours to make it reasonible. Let's ignore the fact that you did not even understand his correction; "The Java compiler is so stupid when it comes to type inference...".
 
@Jean-FrançoisSavard I understood it. With clever compilers, generic types are often automatically inferred. BTW, the corrections I had to make were slight, and I hadn't fully checked the answer yet. About the upvotes, You got one more, mostly because you answered first and then the question was closed, making people lose interest. Whatever, if you think your answer is fine, do as you want, but to me your answer is and will stay terribly bad
 
@Dici AFAIK my answer was -1 (from you) when you posted yours, the fact that I posted first played against me, not for me.
 
Whatever ! If you're done saying defending your one-liner, you can leave me alone now
 
@Dici Last point, you might want to change Java x < 7 fashion for Java x <= 7 fashion in your so-complete answer.
 

last day (14 days later) »