向集合中添加Person类型并对其排序

package com.bjpowernode.t03sort;

import java.util.ArrayList;
import java.util.Collections;

/*
* 向集合中添加Person类型并对其排序
*/
public class TestSort02 {

public static void main(String[] args) {
ArrayList<Person> list = new ArrayList<>();

list.add(new Person(201,"jack"));
list.add(new Person(301,"abel"));
list.add(new Person(101,"curry"));
list.add(new Person(501,"paul"));
list.add(new Person(801,"kidd"));
list.add(new Person(801,"david"));

//排序前
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}

//要想对自定义类型进行排序的话,自定义类型需要实现Comparable接口
Collections.sort(list);
System.out.println("================================");
//排序后
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}

}

}

上一篇:SSH加密原理、RSA非对称加密算法学习与理解


下一篇:Java初学者笔记