Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help - Using mergeSort to sort ArrayList

 
Diana Yu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Diana Yu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Diana Yu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic