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.
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
Joined: Mar 11, 2003
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.
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.
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!!!