I came across the following problem. I have an ArrayList(1) of ArrayLists(2). What I need to do is, to sort the structure so that the first elements of ArrayLists(2) to be in ascending order going down ArrayList(1). To clarify:
Input:
3, 8, 6
2, 14, 205, 44, 1
1, 3
Output:
1, 3
2, 14, 205, 44, 1
3, 8, 6
Look how it sorts the rows only based on the first value.
For now the way the arraylist of arraylists is defined for me is:
List<List<Integer>> graph = new ArrayList<List<Integer>>();
// and I add elements to it likewise
graph.get(currentIndex).add(new ArrayList<Integer>());
The reason I am using ArrayList is because I read that it is more memory efficient than LinkedList and because I am building a list of adjacency lists of a graph. In it both either the number of nodes can vary or the length of the adjacency list per node. The first element of a row is the start_node, the following - its adjacent. Could you please tell me how can I implement this sorting?