Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Comparing objects - cleanest approach

$
0
0
I have a class A which has 5 fields. I want to be able to sort the class based on every field.
Now, i have 2 related question.

First, what is the cleanest solution to provide the sorting functionality. Should i just create 5 .java files and each will be responsible for one field?
i.e.
Spoiler


or should i create 5 classes in 1 .java file?

The second question is, is it possible in Java to delegate the sorting to one of these classes defined above if I dont specify explicitely the class i want the class to be sorted on (wihtout using Comparable)

ie.

Collections.sort(List<A> list) ---> without specifying 2nd parameter - the Comparator - it knows it should sort by FieldTwoSorter. I know Comparable is the solution but i was wondering whether it is possible.

Thanks.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>