'Sorting an ArrayList of ArrayList in lexicographical order

I am having an arraylist of arraylist of subsets of number. According to my code the output is correct but the output is not in lexicographical order. Can anyone tell me how to sort a arraylist of arraylist in lexicographical order?

Here is my code for finding subsets of a number.

 public static ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> A)
    {
        //code here
        ArrayList<ArrayList<Integer>> result=new ArrayList<>();
        ArrayList<Integer> output=new ArrayList<>();
        Solve(A,output,result,0);
        // Collections.sort(result);
        return result;
    }
    public static void Solve(ArrayList<Integer> input,ArrayList<Integer> output,ArrayList<ArrayList<Integer>> result,int start){
        if(start==input.size()){
            result.add(new ArrayList<Integer>(output));
            return;
        }
        output.add(input.get(start));
        Solve(input,output,result,start+1);
        output.remove(output.size()-1);
        Solve(input,output,result,start+1);
       
        
    }

THE EXPECTED OUTPUT IS

INPUT-

size=3
1 2 3

OUTPUT -

1 ,
1 2 , 
1 2 3 , 
1 3 , 
2 , 
2 3 , 
3 ,

but my output is same but not in order

OUTPUT-

1 2 3 , 
1 2  , 
1 3  , 
1  , 
2 3  , 
2  , 
3 ,


Solution 1:[1]

You can use this method

Collections.sort(result, (o1, o2) -> {
  int n = Math.min(o1.size(), o2.size());
  for(int i=0;i<n;i++){
     if(o1.get(i)==o2.get(i)) continue;
     else return o1.get(i) - o2.get(i);
  }
  return 1;
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suraj Rao