Reputation: 101
Assume that I have implemented a class Edge which has 4 attributes, all of which are of type int: from
to
quality
length
.
In my program , I have created an Edge[]
array.
I want to Implement 2 Sorting Parameters -
One of them will sort the Edge array in descending order of the qualities
,
The other will sort based on increasing order of lengths
.
I will be needing these two orderings in distinct parts of my code.
I will be using the library function Arrays.sort()
for sorting.
The only way I know of sorting Class Data Type Arrays to implement compareTo()
within the class Edge but this only works for one parameter(quality or length but not both).
How can I implement two sorting functions(2 compareTo()
functions ?) and decide which one to be called during sorting? In C++ , we can make many compare functions and simply state the function to go through.How to achieve this in Java?
Note: My goal is to sort an array of DataType Edge using Arrays.sort()
, and use two different parameters for sorting and DECIDE which one is to be used at which point.
Upvotes: 0
Views: 199
Reputation: 180113
You want to implement the two sort orders (or at least one of them) by implementing Comparator<Edge>
. You then sort via the version of Arrays.sort()
that accepts a Comparator
as a second parameter.
Do not confuse that with having Edge
itself implement the Comparable
interface. The Comparator
implementations will be different classes (from Edge
and from each other).
Example:
public class DescendingByQuality implements Comparator<Edge> {
public int compare(Edge e1, Edge e2) {
if (e1.getQuality() < e2.getQuality()) return 1;
if (e1.getQuality() > e2.getQuality()) return -1;
return 0;
}
}
...
Edge[] edges = ...;
Arrays.sort(edges, new DescendingByQuality());
Upvotes: 0
Reputation: 178253
In Java, you can create 2 classes that implements Comparator
, defining your compare
method, one for each of your sort orders you desire.
Then you can pass an instance of one of those Comparator
s to Arrays.sort
.
If you're using Java 8, you can pass a method reference to Comparator.comparing
to construct a Comparator
based on a getter method.
Comparator<Edge> lengthAscEdgeComp = Comparator.comparing(Edge::getLength);
To make a descending sort, you can call reversed
.
Comparator<Edge> qualityDescEdgeComp = Comparator.comparing(Edge::getQuality).reversed();
Upvotes: 1
Reputation: 3767
You create two separate Comparator
implementations that perform the algorithms you describe. Then you can pass the appropriate instance to Collections.sort
.
public class EdgeCompareLength implements Comparator<Edge>{
@Override
public int compare(Edge o1, Edge o2) {
// your implementation here
return 0;
}
}
public class EdgeCompareProperties implements Comparator<Edge>{
@Override
public int compare(Edge o1, Edge o2) {
// your implementation here
return 0;
}
}
So someplace you would have
List<Edge> edges = getEdges();
Collections.sort(edges,new EdgeCompareLength());
List<Edge> moreEdges = getMoreEdges();
Collections.sort(edges,new EdgeCompareProperties();
Upvotes: 2