implementing comparable

S

steve

I have a set of data stored in a vector, which is used to display a table on
screen.
the user is allowed to select the table col, and sort the data (ass/dec) as
follows:


colindex= col number
ascending= true/false

Vector data = model.getDataVector();
Collections.sort(data, new
utils.columnSorter(colIndex,ascending));



The actual vector contains an object for each row in the table.
so the basic object may contain a collection of strings/dates/ int


as the interface is standard:
public int compare(Object a, Object b)


so i need to be passed 2 objects into "compare"
this then needs splitting down either into :

1. an object array
2. a vector
so basically i need to take a single object & split it down into a multiple
array, but it needs to be type safe, so it cannot just be converted to an
array of strings.

I would prefer a vector so that i can use

public int compare(Object a, Object b) {

Vector myvector1=a;
Vector myvector2=b;

Object myobject1 =myvector1.get(colIndex);
Object myobject2 =myvector2.get(colIndex);

do instanceof testing/ for string /int/date etc.

can anyone give me a hand on how to split a single object into a multi
object, or into a vector.
 
C

Chris Smith

Steve,

I'm afraid your question is somewhat unclear. I'm not quite sure where
you're starting from. Can you please describe in detail exactly what's
in the vector you mention, and how you intend to identify the selected
"column" of data within the elements?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

steve

Steve,

I'm afraid your question is somewhat unclear. I'm not quite sure where
you're starting from. Can you please describe in detail exactly what's
in the vector you mention, and how you intend to identify the selected
"column" of data within the elements?

one too many actually.
it all stems from the fact that i needed to sort a vector of items .
The vector is used to display an on screen table.
the table has a number of columns. ( which could contain any basic java type
-int,string,date,float- but not complex user defined objects)

The user selects which column , they want to sort on.
the selection is via , some radio buttons, but could just as easily be called
from a double click on the column header


the 'comparable' takes 2 "objects" , which in this case would be 2 vectors
objects containing a collection of strings.

basically you need a vector of vectors , not a vector of strings.
( if you want to expand the comparable, you need to maintain type safety, and
therefore cannot assume you are dealing with strings)

the (OLD) basic code that loads the vector from a database is shown.





java.sql.ResultSetMetaData rsmd = rset.getMetaData();
int ColumnCount = rsmd.getColumnCount();
newmapping_list.removeAllElements(); // empty the supplier list

while (rset.next()) {
String[] record1 = new String[ColumnCount];

for (int i = 0; i < record1.length; i++) {
record1 = (String) rset.getString(i + 1); // COPY
THE DATA TO A LOCAL ARRAY OF THE RIGHT SIZE
}

loop++;

newmapping_list.addElement(record1);
}

whilst this is excellent for on screen display of items , but it is complete
shite for loading a vector of items , that require sorting. ( it will never
work correctly)

I have therefore changed it to:


newmapping_list.removeAllElements(); // empty the supplier list

while (rset.next()) {
Vector record1 = new Vector(); // new String[ColumnCount];

for (int i = 0; i < ColumnCount; i++) {
// record1 = (Object) rset.getObject(i + 1); //
COPY THE DATA TO A LOCAL ARRAY OF THE RIGHT SIZE
record1.add((String) rset.getString(i + 1));
}

loop++;

newmapping_list.addElement(record1);
}

now i can implement my comparable as:

public int compare(Object one, Object two) {
Vector actualRow1 = (Vector) one;
Vector actualRow2 = (Vector) two;

Object ColItem1 = actualRow1.get(colIndex);
Object ColItem2 = actualRow2.get(colIndex);
.........
compare routines based on object types follow.



Which now gives me access to the "actual" col, objects, simply by passing an
index.( from the radio buttons or the double click on the col header)

whereas before I was trying to convert an array of strings back to a vector,
because i am working on a single comparator for all my tables, same as i
have a 'single' table model.

Basically i was fumbling about in the comparator , when the problem was else
where. ( the routine that was getting the actual data from the database)

I just need to change all my "get string routines" to "get vector routines",
which i had originally written , to make the overriding of the java table
objects easier.

that said, i am very surprised at the speed, this can sort several hundreds
of items.


steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top