java List 排序问题

鄙人大概想了三种方式:

1.第一种:借助工具类 Collections 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class TestSet {

	public static void main(String[] ars){
		Person p1 = new Person("a", 20);
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("b", 20));
		persons.add(new Person("a", 20));
		persons.add(new Person("c", 21));
		persons.add(new Person("d", 19));
		persons.add(new Person("e", 19));
		persons.add(new Person("f", 22));
		persons.add(new Person("g", 23));
		persons.add(new Person("h", 18));
		persons.add(new Person("i", 24));
		Collections.sort(persons,new Comparator<Person>(){
			@Override
			public int compare(Person o1, Person o2) {
				
				return o1.getAge()==o2.getAge() ? o2.getName().compareTo(o1.getName()):o2.age-o1.age;
			}
		});
		for (Person p :persons) {
			System.out.println(p.getAge()+":"+p.getName());
		}
	}
}

class Person{
	String name ;
	int age ;
	public Person(String name ,int age){
		this.name = name;
		this.age  = age ;
	}
	
	public int getAge() {
		return age;
	}
	
	public String getName() {
		return name;
	}
}

2:将List 转换数组排序 然后在转换List

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


public class TestSet {

	public static void main(String[] ars){
		Person p1 = new Person("a", 20);
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("b", 20));
		persons.add(new Person("a", 20));
		persons.add(new Person("c", 21));
		persons.add(new Person("d", 19));
		persons.add(new Person("e", 19));
		persons.add(new Person("f", 22));
		persons.add(new Person("g", 23));
		persons.add(new Person("h", 18));
		persons.add(new Person("i", 24));
		Person[] pary = new Person[persons.size()];
		pary =persons.toArray(pary);
		Arrays.sort(pary, new Comparator<Person>(){
			@Override
			public int compare(Person o1, Person o2) {
				return o1.getAge()==o2.getAge() ? o2.getName().compareTo(o1.getName()):o2.age-o1.age;
			}
		});
		persons = Arrays.asList(pary);
		for (Person p :persons) {
			System.out.println(p.getAge()+":"+p.getName());
		}
	}
}

class Person{
	String name ;
	int age ;
	public Person(String name ,int age){
		this.name = name;
		this.age  = age ;
	}
	
	public int getAge() {
		return age;
	}
	
	public String getName() {
		return name;
	}
}

3.利用ListIterator :


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class TestSet {

	public static void main(String[] ars){
		Person p1 = new Person("a", 20);
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("b", 20));
		persons.add(new Person("a", 20));
		persons.add(new Person("c", 21));
		persons.add(new Person("d", 19));
		persons.add(new Person("e", 19));
		persons.add(new Person("f", 22));
		persons.add(new Person("g", 23));
		persons.add(new Person("h", 18));
		persons.add(new Person("i", 24));
		
		//方法三
		Object[] a = persons.toArray();
		Arrays.sort(a,new Comparator<Object>(){
			@Override
			public int compare(Object o1, Object o2) {
				Person p1 = (Person)o1;
				Person p2 = (Person)o2;
				return p1.getAge()==p2.getAge() ? p2.getName().compareTo(p1.getName()):p2.age-p1.age;
			}
		});
		java.util.ListIterator<Person> li = persons.listIterator();
		for (int i = 0; i < a.length; i++) {
			li.next();
			li.set((Person) a[i]);
		}
		for (Person p :persons) {
			System.out.println(p.getAge()+":"+p.getName());
		}
	}
}

class Person{
	String name ;
	int age ;
	public Person(String name ,int age){
		this.name = name;
		this.age  = age ;
	}
	
	public int getAge() {
		return age;
	}
	
	public String getName() {
		return name;
	}
}

此处,不用多说,如果排序规则不同,修改Comparator的比较逻辑即可


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值