Given matrix
ArrayList < ArrayList < Integer > > matrix = new ArrayList < ArrayList < Integer > > ( ) ; ArrayList < Integer > list1 = new ArrayList < Integer > ( Arrays . asList ( 9 , 7 , 4 , 5 ) ) ; ArrayList < Integer > list2 = new ArrayList < Integer > ( Arrays . asList ( 1 , 6 , 2 , - 6 ) ) ; ArrayList < Integer > list3 = new ArrayList < Integer > ( Arrays . asList ( 12 , 20 , 2 , 0 ) ) ; ArrayList < Integer > list4 = new ArrayList < Integer > ( Arrays . asList ( - 5 , - 6 , 7 , - 2 ) ) ; matrix . add ( list1 ) ; matrix . add ( list2 ) ; matrix . add ( list3 ) ; matrix . add ( list4 ) ; sortBoundaryWise ( matrix ) ; } }