wood burning stoves 2.0*
The moose likes Java in General and the fly likes Need Help - Using mergeSort to sort ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Help - Using mergeSort to sort ArrayList" Watch "Need Help - Using mergeSort to sort ArrayList" New topic
Author

Need Help - Using mergeSort to sort ArrayList

Diana Yu
Greenhorn

Joined: Mar 11, 2003
Posts: 3
I need to write a class that sort the student list by last name using mergeSort(if same last name, then by first name).
The requirements are:
mergeSort(ArrayList, int, int):void
merge(ArrayList, int, int, int):void
Can anyone help me out?
Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hello, welcome to JavaRanch. Can I get you to take a look at our display name policy and edit your profile to give us a display name that looks a little more like a real name, rather than an abbreviation or random line noise? Thanks.
If your objective is just to get the list sorted, using Collections.sort() is probably best. This already uses a mergesort with slight modifications. You can use Collections.sort(List,Comparator) to specify a Comparator object which implements a particular sorting criterion. (Like sorting by last name then first name.)
If this is an assignment where the instructor wants to see you write a mergesort yourself, then, well, you really should do most of it yourself. Do you have a text or something describing how a mergesort works? Have you tried writing any code yet? What specific issues or questions do you have?
In either case, you also need to know what exactly is in the ArrayList. Are the objects Strings? Or some other class? If they're Strings, are they stored like "Jim Yingst" or "Yingst, Jim"? This will make a difference in terms of how you implement the sorting criterion. The latter format is really simple to deal with - you can just sort using the natural order of the Strings, since this will sort by last name, then first name.


"I'm not back." - Bill Harding, Twister
Diana Yu
Greenhorn

Joined: Mar 11, 2003
Posts: 3
OK, my display name is changed now and I'd like to continue the discussion of my question.
Yes, this is an assignment which requires us to write a MergeSort method and it must use compareTo() method. The student names are stored like "Yingst,Jim". Below is my code, it can pass compile but give me the wrong results.

[ March 12, 2003: Message edited by: Jim Yingst ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I just scanned your code by eye and saw this:
You have i++ twice, do you need that?
Also the very first "}" needs to be removed to get the listed code to compile. I guess that got in by accident somehow.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Diana Yu
Greenhorn

Joined: Mar 11, 2003
Posts: 3
The code(whole assignment) has been modified as following:

Here is the result I got:
[
First name: Lauren
Last name: Bush
,
First name: Charlie
Last name: Brown
,
First name: George
Last name: Bush
,
First name: Bill
Last name: Clinton
,
First name: Lauren
Last name: Bush
,
First name: George
Last name: Bush
,
First name: Bill
Last name: Clinton
,
First name: Charlie
Last name: Brown
,
First name: Charlie
Last name: Brown
]
Please Help!!!
 
 
subject: Need Help - Using mergeSort to sort ArrayList
 
Similar Threads
MergeSort
Linked List
Merge sort failure
ArrayList sorting
Help with using mergesort to sort a list of names alphabetically?