简体   繁体   中英

How do I sort an ArrayList of Objects?

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.

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);
}

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.

You can use Collections.sort() (it takes an optional comparator if that's of interest).

Note that this will sort your collection in place (ie modify the original), and as such you may wish to take a copy.

Note also the ordering tutorial , which is well worth a read.

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);
     }
});
  1. You can use Collections.sort(nodes) to sort the elements where nodes is a subclass of any collection ie an ArrayList.

  2. The node class can't be static since you want to compare different objects of the class Node.

  3. @Override should be added before the compareTo(Node other) method since it overrides a method declared in the interface.

  4. The class variables should either be public and accessed directly or private and accessed through a method.

  5. 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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM