Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Sorting TableRow in Table

$
0
0
This is one workaround I thought of.... It sort of defeats the purpose of TableRow... If anyone has a better solution or advice, please post it here....

  1.       Table table = new Table();
  2. table.addColumn("id");
  3. table.addColumn("x");
  4. table.addColumn("-x");
  5. table.addColumn("1/x");

  6. for(int i = 1; i <=100; i++){
  7. TableRow newRow = table.addRow();
  8. newRow.setString("id", String.valueOf(i-1));
  9. newRow.setString("x", String.valueOf(i));
  10. newRow.setString("-x", String.valueOf(i*-1));
  11. newRow.setString("1/x", String.valueOf(1/i));
  12. }
  13. //saveTable(table, "data/data.csv");

  14. //convert to array of Table
  15. ArrayList<Table> tables = new ArrayList<Table>();
  16. for(TableRow row: table.rows()){
  17. Table new_table = new Table();
  18. new_table.setColumnTitles(table.getColumnTitles());
  19. new_table.addRow(row);
  20. tables.add(new_table);
  21. }

  22. Collections.sort(tables, new Comparator<Table>(){
  23. @Override public int compare(Table t1, Table t2){
  24. return t1.getInt(0, "-x") - t2.getInt(0,"-x");
  25. }
  26. });

  27. for(Table t:tables){
  28. println(Arrays.toString(t.getStringRow(0)));
  29. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles