/**来自
NowJava.com - 时代Java**/
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Person[] persons = new Person[] {
new Person("A", "AA"),
new Person("B", "BB"),
new Person("C", "CC")};
System.out.println("Before sorting...");
print(persons);
// Sort the persons list
Arrays.sort(persons);
System.out.println("\nAfter sorting...");
print(persons);
}
public static void print(Person[] persons) {
for(Person person: persons){
/**
n o w j a v a . c o m - 时 代 Java
**/
System.out.println(person);
}
}
}
class Person implements Comparable<Person> {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
// Compares two persons based on their last names. If last names are
// the same, use first names
public int compareTo(Person anotherPerson) {
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。