• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help - Using mergeSort to sort ArrayList

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic