Problems with sorting of Timestamps and integers

Hi. I am having some problems when I抦 trying to sort a list of Timestamps and integers.. the thing is that i have a list of Objects(Tasks) containing Timestamps(StartTime and EndTime) and an integer, Priority (0 or 1), the different Tasks are to be sorted after StartTime, EndTime and priority, and the list should only consist of an suggested order to complete the different Tasks..

The tasks can be at the same time, therefore the Priority, and one Task may also start before another Task is completed and vice versa.

(The user can only perform one task at the time)

I have tried for some time now, but I just cant seem to fix this problem..

An example would be very nice.

Thanks / David

[726 byte] By [Darulla] at [2007-10-3 2:57:43]
# 1

> An example would be very nice.

Have your Task implement the Comparable interface:

public class Task implements Comparable<Task> {

// your methods

public int compareTo(Task other) {

if(/* 'this' Task is more important than 'other' Task */) {

return 1;

}

else if(/* 'this' Task is less important than 'other' Task */) {

return -1;

}

else {

return 0;

}

}

// ...

}

Now stuff some Tasks in a Collection, lets say a List, and sort it using the Collections.sort(...) method:

List<Task> tasks = new ArrayList<Task>();

// populate your task-list

Collections.sort(tasks);

Good luck.

prometheuzza at 2007-7-14 20:47:04 > top of Java-index,Other Topics,Algorithms...