本文共 1995 字,大约阅读时间需要 6 分钟。
- package com.jabberchina.test;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
-
- public class SortTest {
-
-
-
- public static void main(String[] args) {
- List<String> lists = new ArrayList<String>();
- List<A> list = new ArrayList<A>();
- List<B> listB = new ArrayList<B>();
- lists.add("5");
- lists.add("2");
- lists.add("9");
-
- Collections.sort(lists);
-
- A aa = new A();
- aa.setName("aa");
- aa.setOrder(1);
- A bb = new A();
- bb.setName("bb");
- bb.setOrder(2);
- list.add(bb);
- list.add(aa);
-
- Collections.sort(list);
-
- B ab = new B();
- ab.setName("ab");
- ab.setOrder("1");
- B ba = new B();
- ba.setName("ba");
- ba.setOrder("2");
- listB.add(ba);
- listB.add(ab);
-
- Collections.sort(listB,new Comparator<B>(){
- @Override
- public int compare(B b1, B b2) {
- return b1.getOrder().compareTo(b2.getOrder());
- }
-
- });
-
- System.out.println(lists);
- System.out.println(list);
- System.out.println(listB);
-
- }
-
- }
-
- class A implements Comparable<A>{
- private String name;
- private Integer order;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getOrder() {
- return order;
- }
- public void setOrder(Integer order) {
- this.order = order;
- }
- @Override
- public String toString() {
- return "name is "+name+" order is "+order;
- }
- @Override
- public int compareTo(A a) {
- return this.order.compareTo(a.getOrder());
- }
-
- }
-
- class B{
- private String name;
- private String order;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getOrder() {
- return order;
- }
- public void setOrder(String order) {
- this.order = order;
- }
- @Override
- public String toString() {
- return "name is "+name+" order is "+order;
- }
- }
-
- 打印的结果为:
- [2, 5, 9]
- [name is aa order is 1, name is bb order is 2]
- [name is ab order is 1, name is ba order is 2]
转载于:https://www.cnblogs.com/uriel/p/5081804.html