My problem is this; I have to order a data table. Each row of the table is an object (allows you to call it TableObject), stored in the list. Each data column is a property of a class (usually a String).
I have to do a typical data order when the user clicks on any column. So I thought about changing the list in TreeSet and implementing Comparator in my TableObject application.
The problem occurs when I try to reorder the TreeSet. Comparison is pretty straightforward at first (cheeks for exceptions in parseInt were omitted):
public int compare(TableObject to1, TableObject to2){ TableObject t1 = to1; TableObject t2 = to2; int result = 1; if(Integer.parseInt(t1.getId()) == Integer.parseInt(t2.getId())){result=0;} if(Integer.parseInt(t1.getId()) < Integer.parseInt(t2.getId())){result=-1;} return result; }
But when I have to reorder by text data or other dozens of data that have a TableObject, I have a problem. I do not want to create dozens of comparison functions, each for one. I prefer not to use a switch (or an ifs chain) to decide how to compare the object.
Is there a way to do this somehow (e.g. Reflexive), which doesnβt mean that I will write like hundreds of lines of almost the same code?
Thanks everyone!
source share