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,564
  • 7
  • 29
  • 47

3 Answers3

3

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

ControlAltDel
  • 32,042
  • 9
  • 48
  • 75
3

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

Jeff Storey
  • 54,892
  • 71
  • 228
  • 402
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,583
  • 2
  • 32
  • 54