2014-03-18 64 views
1

我知道这个问题已被问但我觉得我的是一个特例。我仍然无法弄清楚这一点。如何对Java中ArrayList中的某些项目进行排序?

所以,我需要,如果他们在部门信息系统或帐户到Employee对象通过其年龄排序。

但是,我无法获得代码只是排序员工在部门信息系统或会计,我将如何指定只是按年龄排序他们,只是打印出剩下的?

这里是所有相关代码:

Employee类:

public class Employee { 

private String name; 
private int age; 
private String department; 

public String getDept(){ 
    return department; 
}//end dept 

public void setDept(String dept){ 
    this.department = dept; 
}//end 

public String getName(){ 
    return name; 
}//end name 

public void setName(String n){ 
    this.name = n; 
}//end 

public int getAge(){ 
    return age; 
}//end age 

public void setAge(int a){ 
    this.age = a; 
}//end 



    public String toString(){ 

     return name + " " + age + " " + department; 
    }//end to string 


public Employee (String n,int age,String dept){ 

    this.name = n; 
    this.age = age; 
    this.department = dept; 

}//end employee 
}//end class 

系类:

import java.util.*; 






public class Department implements Comparator<Employee> { 

@Override 
public int compare(Employee o1, Employee o2){ 

    return o1.getAge() - o2.getAge(); 

}//end method 

}//end departmen 

主要方法类:

import java.util.*; 



public class Company { 


public static void main(String [] args){ 


    PrimeAgeChecker p = new PrimeAgeChecker(); 
    ArrayList<Employee> test = new ArrayList<Employee>(); 


    test.add(new Employee("Counting Guru",55,"Accounting")); 
    test.add(new Employee("Counting Pro",45,"Accounting")); 
    test.add(new Employee("Counting Savvy",40,"Accounting")); 
    test.add(new Employee("Counting Novice",25,"Accounting")); 
    test.add(new Employee("Sales Guru",50,"Marketing")); 
    test.add(new Employee("Sales Pro",48,"Marketing")); 
    test.add(new Employee("Sales Savvy",38,"Marketing")); 
    test.add(new Employee("Hiring Guru",58,"Human Resrouces")); 
    test.add(new Employee("Hiring Pro",47,"Human Resrouces")); 
    test.add(new Employee("Hacking Pro",47,"Information Systems")); 
    test.add(new Employee("Hacking Guru",51,"Information Systems")); 
    test.add(new Employee("Hacking Savvy",38,"Information Systems")); 
    test.add(new Employee("Hacking Novice",23,"Information Systems")); 


    for(Employee i: test){ 
    if(i.getDept().equals("Information Systems") ||  i.getDept().equals("Accounting")){ 
     Collections.sort(test, new Department()); 
     System.out.println(i + " " + p.isPrime(i)); 
    }//end 
     else{ 
      System.out.println(i + " " + p.isPrime(i)); 
     } 





}//end main 
}//end company 
+0

你可以显示一小组输入的预期排序输出吗?我能理解你在说什么,但想确认。 – Bhaskar

+0

当然,看看主要的方法类:我想排序Employee对象具有参数(String,integer,“Accounting”)和排序具有参数(String,integer,“Information Systems”)的Employee对象。我不想将其他具有“市场营销”或“人力资源”的对象作为最后一个参数进行排序。 – MrTimotheos

+0

不 - 我要求一个排序输出的例子。请不要以文字标准。 – Bhaskar

回答

0

尝试这个例子,我离开了PrimeAgeCheck出来:

package de.professional_webworkx.blog.companymanager; 

    import de.professional_webworkx.blog.companymanager.comparators.AgeComparator; 
    import de.professional_webworkx.blog.companymanager.filter.DepartmentFilter; 
    import de.professional_webworkx.blog.companymanager.filter.utils.FilterUtils; 
    import de.professional_webworkx.blog.companymanager.model.Employee; 
    import java.util.ArrayList; 
    import java.util.Collections; 
    import java.util.List; 

    public class CompanyManager { 

     public static void main(String[] args) { 
      List<Employee> employees = new ArrayList<>(); 

      employees.add(new Employee("Counting Guru", 55, "Accounting")); 
      employees.add(new Employee("Counting Pro", 45, "Accounting")); 
      employees.add(new Employee("Counting Savvy", 40, "Accounting")); 
      employees.add(new Employee("Counting Novice", 25, "Accounting")); 
      employees.add(new Employee("Sales Guru", 50, "Marketing")); 
      employees.add(new Employee("Sales Pro", 48, "Marketing")); 
      employees.add(new Employee("Sales Savvy", 38, "Marketing")); 
      employees.add(new Employee("Hiring Guru", 58, "Human Resrouces")); 
      employees.add(new Employee("Hiring Pro", 47, "Human Resrouces")); 
      employees.add(new Employee("Hacking Pro", 47, "Information Systems")); 
      employees.add(new Employee("Hacking Guru", 51, "Information Systems")); 
      employees.add(new Employee("Hacking Savvy", 38, "Information Systems")); 
      employees.add(new Employee("Hacking Novice", 23, "Information Systems")); 

      for(Employee employee : employees) { 
       System.out.println(employee); 
      } 

      List<String> filterDepartsments = new ArrayList<>(); 
      filterDepartsments.add("Accounting"); 
      filterDepartsments.add("Information Systems"); 

      List<Employee> applyFilter = FilterUtils.applyFilter(employees, new DepartmentFilter("Accounting", "Information Systems")); 

      System.out.println("filtered list"); 
      for(Employee employee : applyFilter) { 
       System.out.println(employee); 
      } 

      Collections.sort(applyFilter, new AgeComparator()); 
      System.out.println("sorted list"); 
      for(Employee employee : applyFilter) { 
       System.out.println(employee); 
      } 
     } 

    } 

员工

package de.professional_webworkx.blog.companymanager.model; 


public class Employee { 

    private String name; 
    private int age; 
    private String department; 

    public Employee(final String name, final int age, final String department) { 
     this.name  = name; 
     this.age  = age; 
     this.department = department; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public String getDepartment() { 
     return department; 
    } 

    public void setDepartment(String department) { 
     this.department = department; 
    } 


    @Override 
    public String toString() { 
     return name + ", " + age + ", " + department; 
    } 

} 

的IFilter接口

package de.professional_webworkx.blog.companymanager.filter; 

public interface IFilter<T> { 

    public boolean accept(T value); 
} 

简单Departmentfilter

package de.professional_webworkx.blog.companymanager.filter; 

import java.util.ArrayList; 
import java.util.List; 

public class DepartmentFilter<String> implements IFilter<String>{ 

    private final String acceptedDepartment; 
    private final String acceptedDepartment2; 

    public DepartmentFilter(final String department, final String department2) { 
     this.acceptedDepartment = department; 
     this.acceptedDepartment2= department2; 
    } 
    @Override 
    public boolean accept(String value) { 
     return acceptedDepartment.equals(value) || acceptedDepartment2.equals(value); 
    } 

} 

AgeComparator

package de.professional_webworkx.blog.companymanager.comparators; 

import de.professional_webworkx.blog.companymanager.model.Employee; 
import java.util.Comparator; 

public class AgeComparator implements Comparator<Employee>{ 

    @Override 
    public int compare(Employee o1, Employee o2) { 

     return Integer.valueOf(o1.getAge()).compareTo(Integer.valueOf(o2.getAge())); 
    } 

} 

FilterUtils类

package de.professional_webworkx.blog.companymanager.filter.utils; 

import de.professional_webworkx.blog.companymanager.filter.IFilter; 
import de.professional_webworkx.blog.companymanager.model.Employee; 
import java.util.ArrayList; 
import java.util.List; 

public class FilterUtils { 

    private static final List<Employee> filteredList = new ArrayList<>(); 

    public static List<Employee> applyFilter(final List<Employee> list, final IFilter filter) { 

     for(Employee e : list) { 
      if(filter.accept(e.getDepartment())) { 
       filteredList.add(e); 
      } 
     } 

     return filteredList; 
    } 
} 

样本输出

Counting Guru, 55, Accounting 
Counting Pro, 45, Accounting 
Counting Savvy, 40, Accounting 
Counting Novice, 25, Accounting 
Sales Guru, 50, Marketing 
Sales Pro, 48, Marketing 
Sales Savvy, 38, Marketing 
Hiring Guru, 58, Human Resrouces 
Hiring Pro, 47, Human Resrouces 
Hacking Pro, 47, Information Systems 
Hacking Guru, 51, Information Systems 
Hacking Savvy, 38, Information Systems 
Hacking Novice, 23, Information Systems 
filtered list 
Counting Guru, 55, Accounting 
Counting Pro, 45, Accounting 
Counting Savvy, 40, Accounting 
Counting Novice, 25, Accounting 
Hacking Pro, 47, Information Systems 
Hacking Guru, 51, Information Systems 
Hacking Savvy, 38, Information Systems 
Hacking Novice, 23, Information Systems 
sorted list 
Hacking Novice, 23, Information Systems 
Counting Novice, 25, Accounting 
Hacking Savvy, 38, Information Systems 
Counting Savvy, 40, Accounting 
Counting Pro, 45, Accounting 
Hacking Pro, 47, Information Systems 
Hacking Guru, 51, Information Systems 
Counting Guru, 55, Accounting 

也许这可以帮助你。 帕特里克

0
Add this to Your Model 

@Override 
    public int compareTo(Object arg0) { 
     // TODO Auto-generated method stub 
     Employee employee = (Employee) arg0; 
     if(employee.getAge() == 10 && this.getAge() == 10){ 

      return this.getDepartment().compareTo(employee.getDepartment()); 
     } 
     return 0; 
    } 

但如果所有要排序的条目在一起,这只会工作。 并请更改您想要对其进行排序的字段。

相关问题