• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting using Collections?  RSS feed

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Now the question is how do you sort the Person p[] array according to his age.Please do using collections API..Can anyone tell me how do we achieve the above thing.Sample Code provided will be helpful

--
Deepak Lal
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Comparator and Comparable.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may also a homework question -- so it would be better if the OP shows more effort. Give it a shot and try to solve it. Giving "code examples" can be close to doing the homework assignment.

Henry
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry and Others,
I dont know how to solve the above query,can you provide me some hints/Suggestions to solve this ...help provided will be appreciated...

how should i use comparator and comparable in this scenario...
Can you explain in brief?



Please need help !!!
 
Kevin Liauw
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Err i'm a newbie my self so i'm not sure if i'm pointing at the right directions here so please forgive my insolence



i try to use array but it seems there is no sort() method in collections API that take array as arguments (please correct me if im wrong)

[ September 28, 2008: Message edited by: Kevin Liauw ]


[HENRY: Delete Solution.]
[ September 28, 2008: Message edited by: Henry Wong ]
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, thank you for your reply ( ), but I am afraid, we like ranchers to Do Their Own Homework, and we don't like simply giving out answers like that.

Your solution is a good one, but it hinders people's learning to be given complete solutions, so in future, please only give hints to help them work things out themselves.

Please don't get annoyed with me, particularly since you have put lots of work into your reply. And thank you for your helpfulness.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, thank you for your reply ( ), but I am afraid, we like ranchers to Do Their Own Homework, and we don't like simply giving out answers like that.

Your solution is a good one, but it hinders people's learning to be given complete solutions, so in future, please only give hints to help them work things out themselves.

Please don't get annoyed with me, particularly since you have put lots of work into your reply. And thank you for your helpfulness.


My Interpretation: Im the original Poster of this thread and i would Immensely thank Kevin for helping me out .I was not expecting the Source code.neways thanks once again for suggestions and hints that have helped me out.

Moderator: please dont get annoyed with Kevin,,he was just here to help me out.I'm sorry on his behalf.

Thanks a lot all of you guys for your helpfulness.

[edit]Alter spaces to "lose" unnecessary smilie, and change code tags to quote tags. CR[/edit]
[ September 28, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not annoyed with Kevin Liauw; it's more a case of my having to apologise to him. I have already explained why we don't like straight answers; it is actually you who are losing out because you won't remember the solution anything like as well as one you worked out for yourself.

If I thought I had read the solution before you did, I would have edited it like this . . .
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not annoyed with Kevin Liauw; it's more a case of my having to apologise to him. I have already explained why we don't like straight answers; it is actually you who are losing out because you won't remember the solution anything like as well as one you worked out for yourself.


Agreed. Your next homework assignment is going to be harder, and you won't have learned from this one for your next one. Please read this...

http://faq.javaranch.com/java/SearchingForSolutionsHasRepercussions

And I also agree that we are *not* mad at Kevin, but you should be. He took away a learning exercise.


BTW, I deleted the solution, anyway -- as these topics can be searched by future students.

Henry
[ September 28, 2008: Message edited by: Henry Wong ]
 
Kevin Liauw
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh ? i'm sorry i thought Mr Deepak just want to see some sample code about Comparator , i really didn't think it was his homework . I'm truly sorry Mr Deepak .

I'll keep this in mind in the future

Once again i'm truly sorry
[ September 28, 2008: Message edited by: Kevin Liauw ]
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't done anything wrong, Kevin
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
campbell can you please post back the solution...i need it for reference.

(or)

atleast create an FAQ for this article and store it in the FAQ List so that it could be helpful for anyone who visits later.

Please post back the deleted solution....

i will be highly obliged to you if you can do the needful.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
campbell can you please post back the solution...i need it for reference.


Instead of using Kevin's solution as a reference, let's try to make your solution the reference.

Please post the code that you have done so far. What problems are you facing? And we'll give you hints to get you to the solution. (We just won't do your homework. For that you can try www.rentacoder.com)

Henry


[HENRY: Fixed Link -- probably won't be used, but fixed anyway]
[ September 28, 2008: Message edited by: Henry Wong ]
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Let's try to make your solution the reference.


Agree. And I haven't recorded the solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!