How to rearrange elements in a collection of bean for a partciular value

Let's say i have a collection of bean and it's sorted by empld, dept, project with the help of custom comparator written for it and using apache collection ComparatorChain to sort the list of this bean. The bean is as follows.

public class Employee  {

    protected String empId; //alphanumeric e.g.abc123
    protected String empFullName;   
    protected String empAddress;
    protected String dept;
    protected String project;
    protected String customer;

    public String getEmpId() {
        return empId;
    public void setEmpId(String empId) {
        this.empId = empId;
    public String getEmpFullName() {
        return empFullName;
    public void setEmpFullName(String empFullName) {
        this.empFullName = empFullName;
    public String getEmpAddress() {
        return empAddress;
    public void setEmpAddress(String empAddress) {
        this.empAddress = empAddress;
    public String getDept() {
        return dept;
    public void setDept(String dept) {
        this.dept = dept;
    public String getProject() {
        return project;
    public void setProject(String project) {
        this.project = project;

    public String getCustomer() {
        return customer;
    public void setCustomer(String customer) {
        this.customer = customer;


The customer values can be, let's say: Company, Government, University

Now let's say there are thousands of records(beans) in the collection and now what i want is for same empId (which can appear twice) if the customer is Company move that below the same empId with customer name University. The records of customer might not be in order might, so any one can appear first etc.

So basically i want to move if two or more records with same empId and one of them has customer =Company move that to the below the order with same empId e.g.

How can i achieve this swapping/rearranging of records in an efficient and possible thread safe way.


If you want to sort the records in a collection you need to have that object class implements comparator and implement method according to your need.

I solved this problem as follows:

  1. You can create a separate comparator class which implements Comparator and has comparison methods Company, Government, University.

  2. You can create creator comparator methods inside Employee class for Company, Government, University like this:

    public static Comparator COMPARE_BY_COMPANY = new Comparator() { public int compare(Employee o1, Employee o2) {

            if ( == null && == null) return 0;   
            if ( != null && == null) return -1;
            if ( == null && != null) return 1;
            return (;    

Although first approach is better, but second approach doesn't harm either.

Then in the class where you want the custom sorting order, do something like this:

 ComparatorChain chain = new ComparatorChain();

             Collections.sort(EmployeeList, chain);

where ComparatorChain is org.apache.commons.collections.comparators.ComparatorChain.

What the above code will do is it will sort by company then by government and then by university and will finally sort the list and results will appear in that order.

Need Your Help

ASP.Net Log Response to Output File

c# webforms

Occaisionally our office printer craps out on us in the middle of a print job, or someone just forgets to print because they get interrupted. In the good 'ole days, I built up my response using a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.