Results 1 to 9 of 9
- 09-20-2010, 02:16 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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?
-
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!
- 09-20-2010, 03:44 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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?
- 09-20-2010, 04:13 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Why is the argument a Calendar if it's supposed to be a Person Comparator?
- 09-20-2010, 04:46 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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.
- 09-22-2010, 05:01 PM #6
Is there a website that will translate this post to English?hai... even i wnt 2 hw it can be done can any one say wthn an example......
- 09-22-2010, 05:22 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
I think it means "I have a spam link, please click on it."
-
- 09-23-2010, 12:07 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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
-
Default Arguments
By Mark Robert in forum New To JavaReplies: 6Last Post: 08-17-2010, 02:56 PM -
Passign arguments
By ninjalord918 in forum AWT / SwingReplies: 4Last Post: 08-03-2010, 11:11 PM -
How do i do a method twice, but with different arguments?
By Meta in forum New To JavaReplies: 6Last Post: 04-05-2010, 09:58 PM -
Arguments in Main
By CyberFrog in forum New To JavaReplies: 2Last Post: 03-30-2008, 09:37 PM -
repetition of 'arguments'(?)
By Igor in forum New To JavaReplies: 3Last Post: 12-13-2007, 10:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks