Reputation: 8473
I'm quite new to this, so sorry if this is a trite question. I have an ArrayList where Node is a custom class. This is how I have defined it:
static class Node implements Comparable<Node> {
String nodeName;
String[] borderingNodes;
public Node(String nodeName, String[] borderingNodes) {
this.nodeName = nodeName;
this.borderingNodes = borderingNodes;
}
public int compareTo(Node node) {
if(borderingNodes.length > node.borderingNodes.length) {
return 1;
}
if(borderingNodes.length == node.borderingNodes.length) {
return 0;
}
if(borderingNodes.length < node.borderingNodes.length) {
return -1;
}
}
}
Now, I tried doing an Arrays.sort(inputNodes)
where inputNodes is an ArrayList... However I got the error:
no suitable method found for sort(ArrayList<Node>)
Arrays.sort(inputNodes);
How do I correctly do this? My sort btw... has to sort on the size of the borderingNodes array.
Upvotes: 0
Views: 4432
Reputation: 159864
Use Collections.sort(inputNodes)
Arrays.sort
is intended for sorting arrays.
Your current compareTo
method does not return an integer for every code path. You can use Integer.compare
public int compareTo(Node node) {
return Integer.compare(borderingNodes.length, node.borderingNodes.length);
}
Upvotes: 5
Reputation: 657
You can use Collections.sort(nodes) to sort the elements where nodes is a subclass of any collection i.e. an ArrayList.
The node class can't be static since you want to compare different objects of the class Node.
@Override should be added before the compareTo(Node other) method since it overrides a method declared in the interface.
The class variables should either be public and accessed directly or private and accessed through a method.
The comparison in the compareTo(Node other) method can be simplified.
The resulting code would be something like:
class Node implements Comparable<Node> {
private String nodeName;
private String[] borderingNodes;
public Node(String nodeName, String[] borderingNodes) {
this.nodeName = nodeName;
this.borderingNodes = borderingNodes;
}
public int getBorderingNodesLength() {
return borderingNodes.length;
}
@Override
public int compareTo(Node otherNode) {
return Integer.compare(borderingNodes.length, otherNode.getBorderingNodesLength());
}
}
Upvotes: 0
Reputation: 13603
You can implement the compareTo method to make custom comparisons. How to override compareTo:
public int compareTo(Node o)
{
//return should be based on the fields in the class
}
then just go like
Collections.sort(yourList);
Another good way of implementing custom comparator is like it this post here.
Collections.sort(nodeList, new Comparator<Node>(){
public int compare(Node o1, Node o2){
if(o1.nodeName.compareTo(o2.nodeName) == 0)
//implement custom compare based on another field
return o1.nodeName.compareTo(o2.nodeName);
}
});
Upvotes: 0
Reputation: 272417
You can use Collections.sort() (it takes an optional comparator if that's of interest).
Note that this will sort your collection in place (i.e. modify the original), and as such you may wish to take a copy.
Note also the ordering tutorial, which is well worth a read.
Upvotes: 2
Reputation: 1075517
You're tring to sort a List
using a function designed for arrays.
You can use Collections.sort(List)
instead. It's meant for List
s.
Upvotes: 3