Results 1 to 7 of 7
  1. #1
    Tek
    Tek is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default help with an assignment!

    I am given a comparator that sorts the rank of a list of military soldiers in alphabetical order, the code is the following
    Java Code:
    public class SoldierRank implements Comparator<Soldier>{
    
    	public int compare(Soldier left, Soldier right){
    		return left.getRank().compareTo(right.getRank());
    	}
    }
    My job is to modify the code so that it sorts the rank from high to low, like Officer Cadet, 2nd Lieutenant, Lieutenant, Captain, etc.
    But i am completely clueless right now, could somebody help me out? Thanks :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tek View Post
    I am given a comparator that sorts the rank of a list of military soldiers in alphabetical order, the code is the following
    Java Code:
    public class SoldierRank implements Comparator<Soldier>{
    
    	public int compare(Soldier left, Soldier right){
    		return left.getRank().compareTo(right.getRank());
    	}
    }
    My job is to modify the code so that it sorts the rank from high to low, like Officer Cadet, 2nd Lieutenant, Lieutenant, Captain, etc.
    But i am completely clueless right now, could somebody help me out? Thanks :)
    um.... Private Tek, it looks like the code above already does that....

  3. #3
    Tek
    Tek is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    um.... Private Tek, it looks like the code above already does that....
    Thanks for replying Captain Fubarable XD. But no, the code only sorts the rank in alphabetical order like Brigadier, Captain, Colonel, etc

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tek View Post
    Thanks for replying Captain Fubarable XD. But no, the code only sorts the rank in alphabetical order like Brigadier, Captain, Colonel, etc
    I see. So does this mean that rank is a String? Perhaps the best solution is to make Rank an enum or a class that implements Comparable<Rank> so that when you call compareTo on your Ranks, they'll know how to sort themselves.

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I would probably just make the ranks numbers: 1,2,3... all corresponding to a certain rank. That way you can print them out in order very easily.
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by joshdgreen View Post
    I would probably just make the ranks numbers: 1,2,3... all corresponding to a certain rank. That way you can print them out in order very easily.
    But this may make it somewhat difficult to debug later. Best to use enums which were added to the language for just this sort of thing. They already implement the Comparable interface and rank things based on the order of declaration. The toString can allow you to output the results ever so nicely.

  7. #7
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    But this may make it somewhat difficult to debug later. Best to use enums which were added to the language for just this sort of thing. They already implement the Comparable interface and rank things based on the order of declaration. The toString can allow you to output the results ever so nicely.

    Nice to know. I've never dealt with enums before.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Need help with a assignment
    By helpMe.Java in forum New To Java
    Replies: 7
    Last Post: 06-06-2010, 05:49 PM
  2. need help! assignment due by tmr!
    By keycoffee in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 06:45 PM
  3. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 02:27 AM
  4. I am looking for help with an assignment
    By nanoo51969 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 10:41 PM
  5. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •