对其中某元素进行排序
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.List; 5 6 public class test { 7 8 /** 9 * @param args 10 * @return 11 */ 12 13 public static void main(String[] args) { 14 15 TuanInfo tuan1 = new TuanInfo(); 16 tuan1.setAaa(2); 17 tuan1.setBuyer(2); 18 TuanInfo tuan2 = new TuanInfo(); 19 tuan2.setAaa(1); 20 tuan2.setBuyer(1); 21 TuanInfo tuan3 = new TuanInfo(); 22 tuan3.setAaa(3); 23 tuan3.setBuyer(3); 24 List<TuanInfo> list = new ArrayList<TuanInfo>(); 25 list.add(tuan3); 26 list.add(tuan2); 27 list.add(tuan1); 28 for (TuanInfo t : list) { 29 System.out.println(t.getBuyer() + " " + t.getAaa()); 30 } 31 Collections.sort(list, new BuyerCompare()); 32 for (TuanInfo t : list) { 33 System.out.println(t.getBuyer() + " " + t.getAaa()); 34 } 35 36 } 37 } 38 39 class TuanInfo { 40 int buyer; 41 int aaa; 42 43 public int getBuyer() { 44 return buyer; 45 } 46 47 public void setBuyer(int buyer) { 48 this.buyer = buyer; 49 } 50 51 public int getAaa() { 52 return aaa; 53 } 54 55 public void setAaa(int aaa) { 56 this.aaa = aaa; 57 } 58 59 } 60 61 class BuyerCompare implements Comparator<TuanInfo> { 62 public int compare(TuanInfo o1, TuanInfo o2) { 63 if (o1.getBuyer() > o2.getBuyer()) 64 return -1; 65 else 66 return 1; 67 } 68 }
82604119 java 菜鸟群 多多学习 一起进步