Java: sort a list of lists -


i have list of lists of points ie: list<list<point>>. e.g.:

list<point> lp; list<list<point>> llp; llp.add(lp); 

lpwould first line of matrix (x=0). llpis whole matrix.

i want list ascendingly sorted according both x , y of each point. managed internal list sorted not whole list. can me please? thank you. here code sort internal list (ie. first line of matrix) according y.

private void ord_horiz (list<point> tab) {     boolean ordered_horiz = false;     int size = tab.size();      while(!ordered_horiz)     {         ordered_horiz = true;          for(int i=0 ; < size-1 ; i++)         {             if(tab.get(i).y > tab.get(i+1).y)             {                 swap(tab, i, i+1);                 ordered_horiz = false;             }         }         size--;     }    }  private void swap(list<point> tab, int ind1, int ind2)  {     point c;     c = tab.get(ind1);      tab.set(ind1, tab.get(ind2));     tab.set(ind2, c); } 

here code wrote far:

collections.sort(tab,new comparator<list<point>>(){           @override         public int compare(list<point> o1, list<point> o2) {             if(o1 != null && o2 !=null)             {                 point var1 = o1.get(0);                             int var1_x = var1.x;                             int var1_y = var1.y;                 point var2 = o2.get(0);                             int var2_x = var2.x;                             int var2_y = var2.y;                              integer.compare(var1_x, var2_x);                             integer.compare(var1_y, var2_y);                 return /* shall return ? */;                              }             return 0;         }     }); 

here screenshot of console output. want point sorted. http://imageshack.us/scaled/large/515/38zy.png

"i want sort external list according x , y, given x order of lp within llp , y order of elements of each lp in llp."

you should use custom comparator. assume each list within llp has same number of elements, , each list distinct x, following should work:

example

//create data      list<list<point>> llp = new arraylist<list<point>>();      for(int i=0; i< 50; i++)     {         list<point> lp = new arraylist<point>();          for(int j=0; j<50; j++)         lp.add(new point((int)(math.random()*1000), i));          llp.add(lp);     }        // comparators     comparator<list<point>> comparator_rows = new comparator<list<point>>() {          @override         public int compare(list<point> o1, list<point> o2) {             int = o2.get(0).y;             int j = o1.get(0).y;             if (i < j) {                 return 1;             } else if (i > j) {                 return -1;             } else {                 return 0;             }         }      };      comparator<point> comparator_columns = new comparator<point>() {          @override         public int compare(point o1, point o2) {             int = o2.x;             int j = o1.x;             if (i < j) {                 return 1;             } else if (i > j) {                 return -1;             } else {                 return 0;             }         }      };      // sort rows     collections.sort(llp, comparator_rows);      // sort columns     (list<point> element : llp)          collections.sort(element, comparator_columns);      //print elements     int rowindex = 0, columnindex=0;      (list<point> row : llp)      {         (point element : row)          system.out.print("("+element.x+ "," + element.y +")|");          system.out.println();     } 

the above code gets first element of each row, , extracts x coordinate. x coordinate used sort rows.

update

if data mixed up, can use following code uses treemaps put elements supposed be. result jagged array.

//create data          list<list<point>> llp = new arraylist<list<point>>();          for(int i=0; i< 50; i++)         {             list<point> lp = new arraylist<point>();              for(int j=0; j<50; j++)             lp.add(new point((int)(math.random()*1000), (int)(math.random()*1000)));              llp.add(lp);         }           //if data mixed need filter new rows based on x using treemap          treemap<integer, treemap<integer,point>> data = new treemap<integer,treemap<integer,point>>();          (list<point> row : llp)          {             (point element : row)              {                 treemap<integer,point> rowforx;                 if(data.containskey(element.x))                     rowforx = data.get(element.x);                 else                     data.put(element.x, rowforx = new treemap<integer,point>());                         //create specific row in treemap                  rowforx.put(element.y,element);             }                    }           //convert sorted treemap(s) lists          llp.clear();         iterator<entry<integer, treemap<integer, point>>> = data.entryset().iterator();          while(it.hasnext())             llp.add(new arraylist<point>(it.next().getvalue().values()));           //print elements         int rowindex = 0, columnindex=0;          (list<point> row : llp)          {             (point element : row)              system.out.print("("+element.x+ "," + element.y +")|");              system.out.println();         } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -