Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[imp] how can we sort the objects in Collections

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou Rajashekar.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic