3

i want to sort an ArrayList that has the type Node, like this :

 [[country1: null], [name2: null], [city3: null], [region4: null], [name6: null]]

To get the value of Node's name i use the function getNodeName() so

 ArrayNode.get(0).getNodeName()  // return country1

I have looking into collection.sort but i don't know how i will do it, Thank u in advance.

Wassim Sboui
  • 1,692
  • 7
  • 30
  • 48

3 Answers3

3

I think you want java.util.Comparator. You can use that with Collections.sort

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
3

Create a Comparator that sorts by node name, then Collections.sort(list,comparator)

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
3

Use Comparator object to define comparison logic.

Something like that:

ArrayList<Node> nodes;
        Collections.sort(nodes, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.getNodeName().compareTo(o2.getNodeName());
            }
        });
mishadoff
  • 10,719
  • 2
  • 33
  • 55