1 import java.util.Collections; 2 import java.util.Comparator; 3 import java.util.LinkedList; 4 5 class Person{ 6 private float height; 7 private String name; 8 9 Person(float height) 10 { 11 this.height=height; 12 } 13 14 public float getHeight() { 15 return height; 16 } 17 18 public void setHeight(float height) { 19 this.height = height; 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 } 30 31 class PersonHeight implements Comparator<Person>{ 32 33 @Override 34 public int compare(Person e1, Person e2) { 35 if(e1.getHeight() < e2.getHeight()){ 36 return 1; 37 } else { 38 return -1; 39 } 40 } 41 } 42 43 44 public class Question3 { 45 46 public static void main(String[] args) { 47 Person p1=new Person(23.4f); 48 p1.setName("Stud1"); 49 Person p2=new Person(2.34f); 50 p2.setName("Stud2"); 51 Person p3=new Person(34.32f); 52 p3.setName("Stud3"); 53 Person p4=new Person(56.45f); 54 p4.setName("Stud4"); 55 Person p5=new Person(21.4f); 56 p5.setName("Stud5"); 57 60 LinkedList<Person> al=new LinkedList<Person>(); 61 al.add(p1); 62 al.add(p2); 63 al.add(p3); 64 al.add(p4); 65 al.add(p5); 66 67 68 69 Collections.sort(al,new PersonHeight()); 70 for(Person p:al) 71 System.out.println(p.getName()+" "+p.getHeight()); 72 73 } 74 75 }
Collections.sort(al,new PersonHeight());
结果:
Stud4 56.45
Stud3 34.32
Stud1 23.4
Atud5 21.4
Stud2 2.34
1 package com.cn.aug26; 2 3 4 import java.util.Collections; 5 import java.util.LinkedList; 6 7 8 class Person1 implements Comparable <Person1>{ 9 private Float height; 10 private String name; 11 12 Person1(float height) 13 { 14 this.height=height; 15 } 16 17 public Float getHeight() { 18 return height; 19 } 20 21 public void setHeight(float height) { 22 this.height = height; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 @Override 34 public int compareTo(Person1 p) { 35 if(this.getHeight()<p.getHeight()){ 36 return 1; 37 38 }else if(this.getHeight()>p.getHeight()){ 39 return -1; 40 }else{ 41 return 0; 42 } 43 44 } 45 } 46 47 48 public class Question3_1 { 49 50 public static void main(String[] args) { 51 Person1 p1=new Person1(23.4f); 52 p1.setName("Stud1"); 53 Person1 p2=new Person1(2.34f); 54 p2.setName("Stud2"); 55 Person1 p3=new Person1(34.32f); 56 p3.setName("Stud3"); 57 Person1 p4=new Person1(56.45f); 58 p4.setName("Stud4"); 59 Person1 p5=new Person1(21.4f); 60 p5.setName("Stud5"); 61 62 LinkedList<Person1> al=new LinkedList<Person1>(); 63 64 al.add(p1); 65 al.add(p2); 66 al.add(p3); 67 al.add(p4); 68 al.add(p5); 69 70 Collections.sort(al);//降序 71 72 for(Person1 p:al) 73 System.out.println(p.getName()+" "+p.getHeight()); 74 75 } 76 77 }
结果:
Stud4 56.45
Stud3 34.32
Stud1 23.4
Stud5 21.4
Stud2 2.34
Collections.sort(al,Collections.reverseOrder());//升序
结果:
Stud2 2.34
Stud5 21.4
Stud1 23.4
Stud3 34.32
Stud4 56.45
若身高相同,比较姓名,修改compareTo 方法:
1 @Override 2 public int compareTo(Person1 p) { 3 if(this.getHeight()<p.getHeight()){ 4 return 1; 5 6 }else if(this.getHeight()>p.getHeight()){ 7 return -1; 8 }else{ 9 return this.name.compareTo(p.name); 10 } 11 12 }