Results 1 to 9 of 9
  1. #1
    tirwit is offline Member
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default Sorting objects with 2 strings arguments and 2 int arguments

    Hi!

    I have a array list of objects of type person, in which, each one of them, has the vacations days left (int), nș of employee (int), name (string) and department (string).

    I want to sort these objects in the array, by number of vacations days left. What should I use?

    I've seen people advise comparators, compareTo, comparable, collections, etc., so I really don't know how to approach this problem. Can anyone shed some light in this, please?

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

    Default

    Yes, create a Comparator<Person> and in the Comparator's compare method, compare vacation days from one Person object vs the other and return an int based on this comparison (1 if the first Person has more days, -1 if the second Person has more days and 0 if they're the same). For more on how to use a Comparator, you should Google for a tutorial or two. Then if your code doesn't work, please come on back with the code and we can have a look at it.

    Best of luck!

  3. #3
    tirwit is offline Member
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    I have 2 types of objects, the type Person, and the type Calendars. The type person stores the number of available days (and the respective getter of number of days), but the object person is stored inside the type Calendars. The array is made of Calendars objects.

    In the comparator I'm trying to call the number of days getter, but since the argument is Calendars, it doesn't allow me to use the getter from the type Person. How do I work around this?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Why is the argument a Calendar if it's supposed to be a Person Comparator?

  5. #5
    tirwit is offline Member
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    Forget my last post. I'm doing something VERY wrong. I'm gonna try to work this out and I'll be back to you.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    hai... even i wnt 2 hw it can be done can any one say wthn an example......
    Is there a website that will translate this post to English?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    I think it means "I have a spam link, please click on it."

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

  9. #9
    tirwit is offline Member
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    What the hell was that? xD

    Anyhow, I've solved the problem. But it's kinda messy now, so I still need to clean it up before I post it here in a way, other people can read.

    Sorry for not doing it atm, but I'm starting to learn programming on job and I never did any serious programming before (Me -> unemployed physicist... So went to programmer.), so I got LOTS of stuff to do.

    Anyhow, java-forums kicks ass! If it weren't for you I would still be looking at the screen with a stupid look on my face! :D

Similar Threads

  1. Default Arguments
    By Mark Robert in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 03:56 PM
  2. Passign arguments
    By ninjalord918 in forum AWT / Swing
    Replies: 4
    Last Post: 08-04-2010, 12:11 AM
  3. Replies: 6
    Last Post: 04-05-2010, 10:58 PM
  4. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:37 PM
  5. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 11:08 AM

Tags for this Thread

Posting Permissions

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