 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frank Meyer Guest
|
Posted: Wed May 25, 2005 7:29 pm Post subject: Sorting an ArrayList twice... |
|
|
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
|
|
| Back to top |
|
 |
Roland Guest
|
Posted: Wed May 25, 2005 7:46 pm Post subject: Re: Sorting an ArrayList twice... |
|
|
On 25-5-2005 21:29, Frank Meyer wrote:
| Quote: | 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
|
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.util.List,%20java.util.Comparator)>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html#compare(java.lang.Object,%20java.lang.Object)>
--
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ /_/ /
|
|
| Back to top |
|
 |
Sebastian Scheid Guest
|
Posted: Wed May 25, 2005 7:47 pm Post subject: Re: Sorting an ArrayList twice... |
|
|
"Frank Meyer" <f.meyer (AT) 1und1 (DOT) de> schrieb im Newsbeitrag
news:d72jls$vev$1 (AT) online (DOT) de...
| Quote: | 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
|
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
|
|
| Back to top |
|
 |
Frank Meyer Guest
|
Posted: Wed May 25, 2005 8:00 pm Post subject: Re: Sorting an ArrayList twice... |
|
|
Roland,
| Quote: | [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
|
|
| Back to top |
|
 |
Frank Meyer Guest
|
Posted: Wed May 25, 2005 8:05 pm Post subject: Re: Sorting an ArrayList twice... |
|
|
Sebastian,
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|