Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Sorting an input collection

Thread view: 
Paul Morrison - 11 Mar 2005 15:16 GMT
Hi

I want to write a method that takes an input such as:

10:20
10:40
30:20
40:50
40:30
40:20
50:20

and will ouptut the collection in ascending order, i.e:

10:20
10:40
30:20
40:20
40:30
40:50
50:20

It also needs to be capable of sorting letters as well, is it possible to
put the items into an ArrayList and sort them, or does havig the ':' in
there make it difficult?

Any pointers would be much appreciated.

Cheers

--
Paul Morrison
Arnaud Berger - 11 Mar 2005 15:30 GMT
Hi,

This can be done with :
Collections.sort(yourArrayList);

Regards,

Arnaud

> Hi
>
[quoted text clipped - 28 lines]
> --
> Paul Morrison
Paul Morrison - 11 Mar 2005 15:45 GMT
> This can be done with :
> Collections.sort(yourArrayList);

My ArrayList is called result, I have got:

Collections.sort(result);

but at runtime I get this exception:

Exception in thread "main" java.lang.ClassCastException
at java.util.Arrays.mergeSort(Arrays.java:1152)
at java.util.Arrays.sort(Arrays.java:1079)
at java.util.Collections.sort(Collections.java:113)
at EulerGraph.main(EulerGraph.java:37)

The above line is line 37, so I know that this is where the problem is, but
I dont understand why. Is there something else I should be doing?

Cheers

--
Paul Morrison
Arnaud Berger - 11 Mar 2005 15:58 GMT
Hi,

This means that one object at least in your ArrayList does not implement
Comparable
The String class does implement Comparable, so I guess you haven't stored
Strings only in
your ArrayList.

The sort() method does its sorting based on Comparable, so you have to
provide these types of Objects.

Regards,

Arnaud

> > This can be done with :
> > Collections.sort(yourArrayList);
[quoted text clipped - 18 lines]
> --
> Paul Morrison
Tor Iver Wilhelmsen - 11 Mar 2005 15:33 GMT
> It also needs to be capable of sorting letters as well, is it possible to
> put the items into an ArrayList and sort them, or does havig the ':' in
> there make it difficult?

Write a custom implementation of Comparator which interprets the
strings the way you want, and pass that to the variant of
Collections.sort() which takes a Comparator argument.
Chris Smith - 11 Mar 2005 15:34 GMT
> and will ouptut the collection in ascending order, i.e:
>
[quoted text clipped - 9 lines]
> put the items into an ArrayList and sort them, or does havig the ':' in
> there make it difficult?

What exactly is the order you want?  Please don't give examples, but
rather a description.  Your near-aside of "It also needs to be capable
of sorting letters..." changes everything, because it's no longer clear
what you would consider a "correct" answer to be.

To sort the strings that you listed above, a simple sort of an ArrayList
of strings would work fine.  However, that may not extrapolate to other
strings.  You need to provide complete information about what you want,
and someone will be able to help you accomplish it.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Patricia Shanahan - 11 Mar 2005 17:31 GMT
> Hi
>
> I want to write a method that takes an input such as:
...
> and will ouptut the collection in ascending order, i.e:
...
> It also needs to be capable of sorting letters as well,
> is it possible to put the items into an ArrayList and
[quoted text clipped - 6 lines]
>
> -- Paul Morrison

You have three options:

1. Use an existing class that implements Comparable, such as
String. A good approach if, in all cases, you want the
natural String sort order and you don't have additional data
you need to track.

2. Use a class of your own that implements Comparable. That
makes sense if there is a natural sort order.

3. Use a sort that takes a Comparator parameter. Provide a
Comparator that can compare any two objects in your List.

The first two options both require all objects referenced by
the List to not only implement Comparable, but to be
mutually comparable. For example, String and Integer have no
defined order, so:

"aa".compareTo(new Integer(3));

and

new Integer(3).compareTo("aa");

both throw ClassCastException.

For option 3, you can mix any classes you like in the
ArrayList, as long as your Comparator produces a consistent
order for any pair of objects referenced by the List.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.