整数排序你总会吧?因为对象不能像整数一样比大小,所以就弄出个 a.compareTo(b) 来比较 a , b .如果 结果 : < 0 : a< b; ==0 : a==b; >=0 : a>b;用这个默认规则来排序.
/** @version 1.30 2004-02-27 @author Cay Horstmann */import java.util.*;public class EmployeeSortTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000); staff[1] = new Employee("Carl Cracker", 75000); staff[2] = new Employee("Tony Tester", 38000); Arrays.sort(staff); // print out information about all Employee objects for (int i = 0;i<staff.length;i++) { Employee e=staff[i]; System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); } } }class Employee implements Comparable { 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 int compareTo(Object otherObject) { Employee other = (Employee)otherObject; if (salary < other.salary) return -1; if (salary > other.salary) return 1; return 0; } private String name; private double salary; }
简单来说就是想用Arrays.sort()方法来排序对象时~~就需要该对象实现了Comparable类比如String类就实现了Comparable类那我们要对String数组进行排序 String[] s = new String[20]; s[0] = new String("dsasa"); . . . s[19] = new String("fdsdfa");排序的规则就是由compareTo()来决定的你可以用Arrays.sort(s);进行从小到大的排序 也可以用 Arrays.sort(s,Collections.reverseOrder());进行从大到小的排序
import java.util.*; public class CompType implements Comparable{ int i; int j; public CompType(int n1,int n2){ i = n1; j = n2; } public String toString(){ return "[i = "+i+", j = "+j+"]"; } public int compareTo(Object rv){ int rvi = ((CompType)rv).i; int rvj = ((CompType)rv).j; return (i<rvi ? -1 : (i==rvi ? 0:1)); } } 这个就是只比较第一个元素的大小!
< 0 : a< b;
==0 : a==b;
>=0 : a>b;用这个默认规则来排序.
@version 1.30 2004-02-27
@author Cay Horstmann
*/import java.util.*;public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000);
staff[1] = new Employee("Carl Cracker", 75000);
staff[2] = new Employee("Tony Tester", 38000); Arrays.sort(staff); // print out information about all Employee objects
for (int i = 0;i<staff.length;i++)
{
Employee e=staff[i];
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}
}class Employee implements Comparable
{
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 int compareTo(Object otherObject)
{
Employee other = (Employee)otherObject;
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
} private String name;
private double salary;
}
String[] s = new String[20];
s[0] = new String("dsasa");
.
.
.
s[19] = new String("fdsdfa");排序的规则就是由compareTo()来决定的你可以用Arrays.sort(s);进行从小到大的排序
也可以用 Arrays.sort(s,Collections.reverseOrder());进行从大到小的排序
import java.util.*;
public class CompType implements Comparable{
int i;
int j;
public CompType(int n1,int n2){
i = n1;
j = n2;
}
public String toString(){
return "[i = "+i+", j = "+j+"]";
}
public int compareTo(Object rv){
int rvi = ((CompType)rv).i;
int rvj = ((CompType)rv).j;
return (i<rvi ? -1 : (i==rvi ? 0:1));
}
}
这个就是只比较第一个元素的大小!