i have an arraylist of objects, each containing a year/month/day/hour property. what i’m ultimately trying to do, is create a new multidimensional array that contains a group of every object by incrementing hour. so for instance, an example of the final array i’d want is:
array: [0]
[0] 01:04:00, 4-10-09
[1] 01:15:00, 4-10-09
[1]
[0] 02:25:00, 4-10-09
[2]
[0] 06:14:00, 4-10-09
[1] 06:27:00, 4-10-09
[3]
[0] 04:03:00, 4-11-09
[1] 04:11:00, 4-11-09
i’m having a difficult time figuring out to properly sort arraylists in java though. how could i sort each section (year,month,day,hour) within the arraylist in ascending order, before inserting them into an final array?
thanks.
To sort a collection you can make a call like this:
The MyDate class will need to implement the interface Comparable which will require the method “int compareTo(object o)” to be implemented.
In the “compareTo” method, you can then write the code which will call the getters for the year, month, and so on until it figures out whether the current object is less than, equal to or greater than the object “o” being passed into the compareTo method.
Example: