Hi newsgroup,
I am a little swamped with my current Java problem and thought that maybe
you can help me again:
I have a ArrayList that contains Objects called rechteck who have a width
and a height as int variables that can be read out with rechteck.getWidth()
and rechteck.getHeight().
Now I have to sort this list (contains around 100 to 1000 elements) in a
descending order firstly regarding the height and afterwards regarding the
width if there are more than one object with the same height...
What I found out in the Java Doc is that there is no easy solution to sort
an ArrayList but nevertheless it is possible to sort an array. So I
transformed the ArrayList to an Array by
Object[] bOArray = bO.toArray();
whereas bO is my ArrayList. Besides the fact that this produces an array
containing objects and not rechteck objects, it works fine, but I don't have
any clue how to go on. I found that I can use Arrays.sort() for the first
sort routine and the other Arrays.sort(intBegin,intEnd) for the second sort
regarding the widths... but I don't understand the comparable request and
the syntax I have to use... anything I coded just gives nearly every error
that Java seems to have and I found no good tips within the net...
So does anyone know how to sort my list? Or is there maybe even a better
idea than using an array? I thought up some combinations of while and for
loops together with a check if an element is bigger than the second a.s.o.
but this seems to be a very difficult algorithm so I suggest that Java can
support some more convenient ways...
Any help is greatly appreciated! Thanks a lot in advance and best regards
Frank
Roland - 25 May 2005 20:46 GMT
> Hi newsgroup,
>
[quoted text clipped - 32 lines]
>
> Frank
Collections.sort(yourList, yourRechteckComparator);
where
Comparator yourRechteckComparator = new Comparator() {
public int compare(Object o1, Object o2) {
return compare( (Rechteck)o1, (Rechteck)o2);
}
public int compare(Rechteck r1, Rechteck r2) {
int diff = r1.getHeight() - r2.getHeight();
if (diff == 0) {
//:: height of both rectangles are equal
// compare widths instead
diff = r1.getWidth() - r2.getWidth();
}
return diff;
}
};
[Untested, and assuming that elements in your list are of type Rechteck]
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.uti
l.List,%20java.util.Comparator)>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html#compare(java.l
ang.Object,%20java.lang.Object)>

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
Frank Meyer - 25 May 2005 21:00 GMT
Roland,
> [Code]
> [Untested, and assuming that elements in your list are of type Rechteck]
thanks a lot... I've roughly checked it and it seems to work fine... I will
try to understand it tomorrow ;o)
Very best regards
Frank
Sebastian Scheid - 25 May 2005 20:47 GMT
> Hi newsgroup,
>
[quoted text clipped - 12 lines]
> What I found out in the Java Doc is that there is no easy solution to sort
> an ArrayList but nevertheless it is possible to sort an array. So I
With Collections.sort() you can sort a List. You should let your class
Rechteck implemente the interface Comparable (or you can give the sort()
method an additional Comparator-object). Read the API doc for these
interfaces.
Regards
Sebastian
Frank Meyer - 25 May 2005 21:05 GMT
Sebastian,
> With Collections.sort() you can sort a List. You should let your class
> Rechteck implemente the interface Comparable (or you can give the sort()
> method an additional Comparator-object). Read the API doc for these
> interfaces.
thanks for the help - I think it works now...
Best
Frank