LinkedList 實現排序

來源:互聯網
上載者:User

標籤:style   blog   color   java   io   strong   for   ar   div   

 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     @Override34     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     @Override34     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

 

 

LinkedList 實現排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.