List---Java对List进行排序

此处以"sitenumber"字段为例
List数据如下:
List> list= new ArrayList>();

[
	{datapath=zx, sitenumber=6, sitename=资讯, siteid=8, id=8, parentid=0}, 
	{datapath=appzd, sitenumber=5, sitename=APP站点, siteid=10, id=10, parentid=0}, 
	{datapath=app_sy, sitenumber=3, sitename=首页, siteid=7, id=7, parentid=0}, 
	{datapath=APP, sitenumber=20, sitename=APP1, siteid=9, id=9, parentid=0}, 
	{datapath=hdqzf, sitenumber=10, sitename=文章, siteid=5, id=5, parentid=0}, 
	{datapath=spwj, sitenumber=1, sitename=视频文件, siteid=4, id=4, parentid=0}
]

实现排序代码,根据sitenumber字段从小到大排列:

Collections.sort(list, new Comparator>() {
				public int compare(Map o1, Map o2) {
					if(Integer.parseInt(o1.get("sitenumber").toString())

实现排序代码,根据sitenumber字段从大到小排列,只需要把如上代码中"<“符号变为”>"符号即可:

Collections.sort(list, new Comparator>() {
				public int compare(Map o1, Map o2) {
					if(Integer.parseInt(o1.get("sitenumber").toString())>Integer.parseInt(o2.get("sitenumber").toString())) {
						return -1;  
					}else if(Integer.parseInt(o1.get("sitenumber").toString())==Integer.parseInt(o2.get("sitenumber").toString())) {
						return 0;  
					}else {
						return 1;
					}
				}
			});

你可能感兴趣的:(Java)