Arrange Data by Lowest First

Hi, I am developing a little program for myself and I have come to a problem.. I have been using an RecordList to store and arrange my data, this has been working until now I need to sort out numerical data. I want to sort the data so the lowest amount is first. The code I am using is:

Code:

public Record(String n, String s, String f, String ty, String ti,String stBreakFast, String stDinner, String stAfternoon, String stTea, String stEvening){

name = n;

start = s;

finish = f;

type = ty;

till = ti;

sBreakFast = stBreakFast;

sDinner = stDinner;

sAfternoon = stAfternoon;

sTea = stTea;

sEvening = stEvening;

keys =new String[5];

keys[0] = name; keys[1]= start; keys[2] = finish;

keys[3] = type; keys[4] = till;

}

public String toString(){

Formatter f =new Formatter();

f.format("%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s", name, start, finish, type, till, sBreakFast, sDinner, sAfternoon, sTea, sEvening);

return f.toString();

}

}

class RecordComparatorimplements Comparator<Record>{

int sortKey;

public RecordComparator(int sk){

if (sk < 0 || sk > 4){

sortKey = 0;

}

else{

sortKey = sk;

}

}

public RecordComparator(){

this(0);

}

publicint compare(Record r1, Record r2){

String s1 = r1.keys[sortKey];

String s2 = r2.keys[sortKey];

return s1.compareTo(s2);

}

}

The start variable is the one I would like to sort out as double..

Anyone with any idea抯?

Thanks

Coops

[2717 byte] By [Coopera] at [2007-11-26 21:12:00]
# 1
I think this one can help you http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
rym82a at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for that, but I don't see how it helps.. I'm still stuck.ThanksCoooper
Coopera at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...
# 3

You do realize that you have to call Collections.sort at some point, right?

That'll probably happen in your RecordList class somewhere, although it's hard to say since you didn't post it.

You do realize that a class you define yourself won't be part of the standard API and so we won't know what it is unless you show it to us, right?

Why are you doing that weirdness with the keys array? If you want to sort on different keys, usually it's easier to create different Comparators. Also are all those fields of Record really Strings? "start" and "finish" seem like they might be Dates.

paulcwa at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...
# 4

I use the main Class/Method stated in the first post and i use the below line when i want to sort the data.

Collections.sort(rlist, new RecordComparator(Record.TILL));

The start would be a Start time e.g. 08.00 or 21.00. It come from an XML document..When i use

I tried to use the same method to arrange the start time as stated below

Collections.sort(rlist, new RecordComparator(Record.START));

But this does not sort it correctly... I think it is because it store as a String... I need to find out if there a way to amend the methods already in use or create another one.. Please Help

Thanks

Coopera at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...
# 5

...and presumably Record.TILL is an integer to index the keys array.

A few things can be going wrong.

Maybe Record.TILL is actually the wrong number. Does the data end up sorted by the wrong field?

Maybe the "till" values are not what you think they are. That kind of thing happens easily when you don't really parse the values and just store everything as strings. Generally speaking you're better off if the class's state is implemented with values whose types are close to the real-world information that the fields represent. So if the value is a time of day, then maybe you could store it as an int representing minutes since midnight, for example. The way to fix this is simply to make the types more appropriate, and when you initialize the data (with Strings, most likely, if you're reading from XML) that you convert it to the correct types.

Anyway, why don't you put some debugging code in the comparator?

paulcwa at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...
# 6
Solved the prob.. it was because the numbers where in 24 hr clock and 800 was no 0800.. So when the org method was sorting the nums it would see 1000 lower than 800, but now that i have changed the 800 to 0800 its working.. Thanks
Coopera at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...
# 7
Yeah that's the kind of case where keeping values in such a format, that's far from what they really represent, can be a problem. If they were ints, this wouldn't have happened.
paulcwa at 2007-7-10 2:49:29 > top of Java-index,Java Essentials,New To Java...