當前位置:成語大全網 - 新華字典 - 壹個數字很亂的數組,怎樣用java來降序排列?

壹個數字很亂的數組,怎樣用java來降序排列?

我原來給別人做了壹個ArrayList的對象的排序,

這裏是按照薪水和名字進行排序的各個實現

很有參考價值!源碼如下...

package 別人的程序的分析設計;

/**

* @author greatwqs

*

*/

import java.util.Collections;

import java.util.*;

public class myQuiz{

public static void main(String[] args) {

Employee e1 = new Employee("Cay",4000);

Employee e2 = new Employee("Bush",6000);

Manager m1 = new Manager ("Alice",5000);

//m1.setBonus(100000);

List<Employee> list_1=new ArrayList<Employee>();

list_1.add(e1);

list_1.add(e2);

list_1.add(m1);

System.out.println("以下按薪水排序後輸出[從高到低]");

Collections.sort(list_1,new Qing());

for(Employee e:list_1)

System.out.println(e);

List<Employee> list_2 = new ArrayList<Employee>();

list_2.add(e1);

list_2.add(e2);

list_2.add(m1);

System.out.println("以下按照姓名字典順序輸出:");

Collections.sort(list_2 , new Song());

for(Employee em :list_2){

System.out.println(em);

}

}

}

class Qing implements Comparator{

public int compare(Object obj0, Object obj1){

Employee c0 = (Employee)obj0;

Employee c1 = (Employee)obj1;

if(c0.getSalary() >= c1.getSalary())

return -1;

else

return 1;

}

}

class Song implements Comparator{

public int compare(Object obj0, Object obj1){

Employee c0 = (Employee)obj0;

Employee c1 = (Employee)obj1;

return c0.getName().compareTo(c1.getName());

}

}

/******Employee and Manager *****/

class Employee

{

public Employee(String n, double s)

{

name = n;

salary = s;

}

public String getName()

{

return name;

}

public double getSalary()

{

return salary;

}

public void raiseSalary(double byPercent)

{

double raise = salary * byPercent / 100;

salary += raise;

}

public String toString(){

return("name: "+name+"salary: "+salary);

}

private String name;

private double salary;

}

//後面還有補充.....