java - Sort array by length of sub arrays -
java - Sort array by length of sub arrays -
so have 2d array graph
represents adjacency list:
0:2 1 0 1:0 2:
in array format thats:
[[2,1,0],[0],[]]
what want sort specific row (say graph[0]
) length of sub array (the border list).
in illustration above sorted graph
like:
0:0 1 2 1:0 2: [[0,1,2],[0],[]]
since graph[0].length = 3
, graph[1].length = 1
, graph[2].length = 0
.
i've tried using:
arrays.sort(graph[v], new degreecomparator(graph)); class degreecomparator implements comparator<integer> { int[][] graph; public degreecomparator(int[][] g) { graph = g; } public int compare(integer c1, integer c2) { homecoming integer.compare(graph[c1].length, graph[c2].length); } }
but sort method won't take format. can explain i'm doing wrong?
edit clarity:
because above illustration uses numbers it's bit confusing i'll add together sec case:
0: 4 1 2 1: 1 2 3 2: 3: 4 1 4: 0 [[4,1,2],[1,2,3],[],[4,1],[0]]
would become (if rows sorted):
0: 1 4 2 // row 1 has more numbers row has more row 2 1: 1 3 2 2: 3: 1 4 4: 0 [[1,4,2],[1,3,2],[],[1,4],[0]]
however, need sort 1 row @ time! not whole thing.
you inquire int[] sorted comparator
change type of graph integer[][]
class degreecomparator implements comparator<integer> { integer[][] graph; public degreecomparator(integer[][] g) { graph = g; } public int compare(integer c1, integer c2) { homecoming graph[c2].length - graph[c1].length; } }
java sorting comparator adjacency-list
Comments
Post a Comment