aspose file tools
The moose likes Beginning Java and the fly likes Sorting using Collections? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Sorting using Collections?" Watch "Sorting using Collections?" New topic
Author

Sorting using Collections?

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507




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


When The Going Gets Tougher,The Tougher gets Going
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
Have a look at Comparator and Comparable.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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

Joined: Aug 28, 2007
Posts: 8
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 ]

Confucius once said:<br />"by three way we may obtain wisdom:<br />1. Reflection , which is the noblest<br />2. Imitation , which is the easiest<br />3. Experience , which is the bitterest "
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
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

Joined: Jul 01, 2008
Posts: 507

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
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
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

Joined: Sep 28, 2004
Posts: 16815
    
  19

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

Joined: Aug 28, 2007
Posts: 8
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
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
You haven't done anything wrong, Kevin
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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

Joined: Sep 28, 2004
Posts: 16815
    
  19

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
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Originally posted by Henry Wong:
Let's try to make your solution the reference.


Agree. And I haven't recorded the solution.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Sorting using Collections?
 
Similar Threads
Important Java Questions
Vector Question
inheritance doubt
Garbage collection - Isolating a Reference
Generics - how to create a universal strategy?