回 帖 发 新 帖 刷新版面

主题:帮帮看这个排序

1975-1982 Konrad Adenauer 
1959-1969 Willy Brandt
1969-2075 Theodor Heuss
分别是任职时间和名字,还有几个没写。按三种排序方法输出,一是按年代输出,二是按字母输出,三是按任职的时间长短输出,我只会按年代输出,现谢谢大家!

回复列表 (共3个回复)

沙发

可以用多种方法,我的 只 做个参考

package bxb_dream;

public class Employee {

    private String holdTime;
    private Integer totalTime;
    private String name;
    
    public Integer getTotalTime() {
        return totalTime;
    }
    public void setTotalTime(Integer totalTime) {
        this.totalTime = totalTime;
    }
    public String getHoldTime() {
        return holdTime;
    }
    public void setHoldTime(String holdTime) {
        this.holdTime = holdTime;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

板凳

package bxb_dream;

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

public class Sort {
    
    public static List<Employee> employees = new ArrayList<Employee>();
    
    
    static{
        
        Employee employee = new Employee();
        employee.setHoldTime("1975-1982");
        employee.setName("Konrad Adenauer ");
        employees.add(employee);
        
        employee = new Employee();
        employee.setHoldTime("1959-1969");
        employee.setName("Willy Brandt");
        employees.add(employee);
        
        employee = new Employee();
        employee.setHoldTime("1969-2075");
        employee.setName("Theodor Heuss");
        employees.add(employee);
        
        employee = new Employee();
        employee.setHoldTime("1969-2075");
        employee.setName("Taeodor Heuss");
        employees.add(employee);
    }
    
    //获得员工就职总时间,并返回
    public static List<Employee> getHoldTime(List employees){
        List<Employee> list = new ArrayList<Employee>();
        for(Object obj : employees){
            Employee employee = (Employee)obj;
            
            //获得开始时间
            Integer startTime = Integer.valueOf(employee.getHoldTime().substring(0, 4));
            //获得结束时间
            Integer endTime = Integer.valueOf(employee.getHoldTime().substring(5, 9));
            
            Integer totalTime = endTime - startTime;
            
            employee.setTotalTime(totalTime);
            
            list.add(employee);
        }
        return list;
    }
    
    //根据员工就职时间排序,并返回
    public static List<Employee> sortByTotalTime(List<Employee> employees){
        
        for(int i = 0; i<employees.size() - 1; i++){
            
            for(int j = i+1; j<employees.size(); j++){
                
                Employee employee = employees.get(i);
                
                Employee empl = employees.get(j);
                
                if(employee.getTotalTime() < empl.getTotalTime()){
                    employees.set(i, empl);
                    employees.set(j, employee);
                }
            }
        }
        
        return employees;
    }

3 楼


    
    
    //根据员工字母排序(忽略大小写),并返回
    public static List<Employee> sortByLetter(List<Employee> employees){
        
        /*String stra = "A";  //65
        String strz = "Z";      //90    
        String str = " ";   //32
        System.out.println(stra.codePointAt(0));
        System.out.println(strz.codePointAt(0));
        System.out.println(str.codePointAt(0));*/
        
        //将员工姓名均变为大写
        /*for(Object obj : employees){
            Employee employee = (Employee)obj;
            employee.setName(employee.getName().toUpperCase());
        }*/
        
        //开始排序
        for(int i = 0 ; i<employees.size()-1;i++){
            for(int j = i+1 ; j<employees.size() ; j++){
                Employee employee = employees.get(i);
                int length2 = employee.getName().length();
                
                Employee empl = employees.get(j);
                int length3 = employee.getName().length();
                
                //取长度较短的,以免索引越界
                int length = length2>length3?length3:length2;
                
                for(int k = 0; k<length ; k++){
                    
                    //全部变为大写
                    String strEmployee = employee.getName().substring(k).toUpperCase();
                    String strEmpl = empl.getName().substring(k).toUpperCase();
                    
                    System.out.println(employee.getName() + " : " + empl.getName());
                    
                    System.out.println(strEmployee.codePointAt(0) + " : " + strEmpl.codePointAt(0));
                    if(strEmployee.codePointAt(0) == strEmpl.codePointAt(0)){
                        
                        continue;
                        
                    }else if(strEmployee.codePointAt(0)>strEmpl.codePointAt(0)){
                        employees.set(i, empl);
                        employees.set(j, employee);
                        break;
                    }else{
                        break;
                    }
                }
                
            }
        }        
        
        return employees;
    }
    
    
    public static void main(String[] args) {            
        
        //按任职时间
        employees = getHoldTime(employees);
        
        employees = sortByTotalTime(employees); 
        
        for(Object obj : employees){
            Employee employee = (Employee)obj;
            System.out.println(employee.getTotalTime() + " : " + 
                    employee.getHoldTime() + " : " + employee.getName());
        }
        
        System.out.println("---------------------");
        
        //按字母
        employees =sortByLetter(employees);
        
        for(Object obj : employees){
            Employee employee = (Employee)obj;
            System.out.println(employee.getTotalTime() + " : " + 
                    employee.getHoldTime() + " : " + employee.getName());
        }
    }
    
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册