List排序

阅读更多
package com.nstc.test;

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

/**
 * Java提供的List排序方法,是调用了List的父类Collections类中的Sort方法
 * Collections.sort()提供两个重载方法:
 * Collections.sort(list);(Java提供的默认排序方法)
 * Collections.sort(list, Comparator);(可以自定义排序方法)
 * 
 * @author changbl
 *
 */
public class TestSort {
	
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add("王硕");
		list.add("李明");
		list.add("刘迪");
		list.add("刘布");
		//默认排序方法
		System.out.println("---------------默认排序方法开始-----------------");
		Collections.sort(list);
		for(String name : list){
			System.out.print(name+"\t");
		}
		System.out.println();
		System.out.println("---------------默认排序方法结束-----------------");
		//利用Collator根据语言排序
		System.out.println("---------------根据java.util.Locale.ENGLISH排序开始-----------------");
		Collections.sort(list,Collator.getInstance(java.util.Locale.ENGLISH));
		for(String name : list){
			System.out.print(name+"\t");
		}
		System.out.println();
		System.out.println("---------------根据java.util.Locale.CHINA排序开始-----------------");
		Collections.sort(list,Collator.getInstance(java.util.Locale.CHINA));
		for(String name : list){
			System.out.print(name+"\t");
		}
		System.out.println();
		System.out.println("---------------根据java.util.Locale.CHINA排序结束-----------------");
		System.out.println();
		System.out.println("【暂时告一段落】");
		System.out.println();
		List list2 = new ArrayList();
		list2.add(5);
		list2.add(87);
		list2.add(54);
		list2.add(290);
		Collections.sort(list2, new SequenceRule());
		System.out.println("----------------实现自定义排序方法1:----------------");
		for(Integer i : list2){
			System.out.print(i+"\t");
		}
		System.out.println();
		System.out.println("----------------实现自定义排序方法1结束----------------");
	}
}

/**
 * 实现自定义排序方法1:
 * 实现Comparator接口,重写compare方法
 * @author changbl
 *
 */
class SequenceRule implements Comparator{
	@Override
	public int compare(Object o1, Object o2) {
		Integer i1 = (Integer)o1;
		Integer i2 = (Integer)o2;
		return i1.compareTo(i2);
	}
}

/**
 * 实现自定义排序方法2:
 * 实现Comparable接口,重写compareTo方法
 * @author changbl
 *
 */
class SequenceRule2 implements Comparable{
	@Override
	public int compareTo(Object o) {
		return 0;
	}
}
 
  

对象实现Comparable接口的代码如下:

public class Person implements Comparable {
    private String name;
    private Integer order;
 
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
 
    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /**
     * @return the order
     */
    public Integer getOrder() {
        return order;
    }
 
    /**
     * @param order
     *            the order to set
     */
    public void setOrder(Integer order) {
        this.order = order;
    }
 
    @Override
    public int compareTo(Person arg0) {
        return this.getOrder().compareTo(arg0.getOrder());
    }
 
}

 

public static void main(String[] args) {
    List listA = new ArrayList();
    Person p1 = new Person();
    Person p2 = new Person();
    Person p3 = new Person();
 
    p1.setName("name1");
    p1.setOrder(1);
    p2.setName("name2");
    p2.setOrder(2);
    p3.setName("name3");
    p3.setOrder(3);
 
    listA.add(p2);
    listA.add(p1);
    listA.add(p3);
    Collections.sort(listA);
    for (Person p : listA) {
        System.out.println(p.getName());
    }
}

 

你可能感兴趣的:(java,list,Collections,排序,list排序)