Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use own implemented sorting method. For this you also need to learn ArryList class methods for swaping values. You'll have to use get(int) and set(int, E) extensively to swap elements.</p> <p>I am showing you one solution that I like to do when Collections.Sort is not feasible. In this I am using "Bubble Sort" sorting method. I hope you are aware from "Bubble Sort".. package online.solution;</p> <pre><code>import java.util.ArrayList; public class Node { private String key; private int x; public Node(String key, int x) { this.key = key; this.x = x; } public int getX() { return x; } public static void main(String[] args) { Node n1 = new Node("B", 14); Node n2 = new Node("H", 7); Node n3 = new Node("U", 13); Node n4 = new Node("S", 1); Node n5 = new Node("H", 88); Node n6 = new Node("A", 99); Node n7 = new Node("N", 12); ArrayList&lt;Node&gt; nodes = new ArrayList&lt;&gt;(); nodes.add(n1); nodes.add(n2); nodes.add(n3); nodes.add(n4); nodes.add(n5); nodes.add(n6); nodes.add(n7); System.out.println("Before sorting..."); for (Node node : nodes) { System.out.println(node.getX()); } int n, i, j; Node swap; n = nodes.size(); for (i = 0; i &lt; (n - 1); i++) { for (j = 0; j &lt; n - i - 1; j++) { if (nodes.get(j).getX() &gt; nodes.get(j + 1).getX()) { swap = nodes.get(j); nodes.set(j, nodes.get(j + 1)); nodes.set(j + 1, swap); } } } System.out.println("After sorting in Ascending Order..."); for (Node node : nodes) { System.out.println(node.getX()); } for (i = 0; i &lt; (n - 1); i++) { for (j = 0; j &lt; (n - i - 1); j++) { if (nodes.get(j).getX() &lt; nodes.get(j + 1).getX()) { swap = nodes.get(j); nodes.set(j, nodes.get(j + 1)); nodes.set(j + 1, swap); } } } System.out.println("After sorting in Descending Order ..."); for (Node node : nodes) { System.out.println(node.getX()); } } } </code></pre> <p>I hope this solution help you in your work...</p> <p><img src="https://i.stack.imgur.com/YjzyS.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload