aspose file tools*
The moose likes Java in General and the fly likes [imp] how can we sort the objects in Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "[imp] how can we sort the objects in Collections" Watch "[imp] how can we sort the objects in Collections" New topic
Author

[imp] how can we sort the objects in Collections

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
class student{
int id;

}

List list=new ArrayList();
list.add(student);

i have added 10 student objects to list.

so ,i want to sort them in ascending order by student id;

can you help me.
Rajasekar Krishnan
Greenhorn

Joined: Feb 27, 2008
Posts: 16

Hi shyam,

You can use
1) <T> void java.util.Collections.sort(List<T> list) to sort the ArrayList, if your the student objects implements Comparable. or
2) <T> void java.util.Collections.sort(List<T> list, Comparator<? super T> c), if you have Comparator to compare Student Objects.

Thanks,
Rajasekar.
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
thanks rajashekar,

my code is like this:

class Student{
int id;

}
Student std1=new Student();
Student std2=new Student();
Student std3=new Student();

List ls=new ArrayList();
ls.add(std1);
ls.add(std1);
ls.add(std1);




so can you help me with code how to sort in ascending order.




Rajasekar Krishnan
Greenhorn

Joined: Feb 27, 2008
Posts: 16

Hi Shyam,



this is one way of doing the sorting in java, We can try out Comparator if number of sorting method required by you.

Thanks,
Rajasekar.
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
thankyou Rajashekar.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Rajasekar Krishnan wrote:Hi Shyam,

. ..
Not a good way to do it. You should be using genericsAnd of course, if there is a great difference between the ids you will have problems

and shyam sunder prasad, welcome to the Ranch
Rajasekar Krishnan
Greenhorn

Joined: Feb 27, 2008
Posts: 16

Hi Campbell,

Thanks for your comment.

Form my next post onwards I'll post the code with Generics (when Required).

thanks,
Rajasekar.
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
thanks for your reply.
your telling me that comparing two objects but i want to put them in ascending order in list as well.
tell me how.
Joshua Barrett
Greenhorn

Joined: Sep 08, 2010
Posts: 27
Write a class that implements Comparator.
This class should have a single method called compare, that returns an int, and takes 2 Object objects as parameters.
Inside this method Cast the Objects to your classes within your list. And write some code that compares the fields you want to sort by.
Basically if Object 1 should come before Object 2 you should return -1.

Check the API of the Interface if you are unsure what methods are required and what they should return.

Then use this static method to sort your list using the given comparator object

Collections.sort(yourList, new YourComparator());

Perhaps not the most efficent way, but it works, and its simple to understand.


This was the best way I could describe what you have to do without just giving you the code
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Joshua Barrett wrote: . . . Cast the Objects to your classes within your list. . . .
Surely you write a class which implements the Comparator<Student> interface, then you can dispense with those casts.
Joshua Barrett
Greenhorn

Joined: Sep 08, 2010
Posts: 27
Campbell Ritchie wrote:
Joshua Barrett wrote: . . . Cast the Objects to your classes within your list. . . .
Surely you write a class which implements the Comparator<Student> interface, then you can dispense with those casts.


Blimeh, that would do away with the extra class! Never knew that existed. Ya learn something new every day
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Joshua Barrett wrote: . . . Ya learn something new every day
That's why I came here in the first place. I have learned lots and lots here.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [imp] how can we sort the objects in Collections