Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

sorting of a list having bean object

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to sort a List having bean CTVo based on its group Name.
Could any body provide me a solution using Comparator.

import java.util.*;

public class Test {
public static void main(String[] args){
List list = new LinkedList();
CTVo vo = new CTVo();
vo.setGroupId("1");
vo.setGroupId("Fill");
list.add(vo);

vo = new CTVo();
vo.setGroupId("1");
vo.setGroupId("Fill");
list.add(vo);

vo = new CTVo();
vo.setGroupId("1");
vo.setGroupId("Fill");
list.add(vo);

Collections.sort(list);
for(int i=0;i<list.size();i++){
CTVo result = new CTVo();

System.out.println(result.getGroupName());
}
}
}
class CTVo{
private String groupId;
private String groupName;
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the comparable or the comparator interface.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Anupam. Either make your CTVo class implement the Comparable interface, or make a custom Comparator which you will pass to sort(List list, Comparator c).
Look for documentation about those classes, and try to figure out yourself how it works. Ask more if you still don't understand how Comparator/Comparable work
Here is a place you could start with : http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please keep to the topic of this forum, SCJP. We have our Java In General forums for, you guessed it, general programming problems.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic