Java Arraylist 如何使用 Comparator排序

时间:2021-01-03 02:52:05

Java Arraylist 如何使用 Comparator排序import java.util.ArrayList; 

import java.util.Collections;
import java.util.Comparator;

public class Sort implements Comparator<String> {

	private final static int UP = 1;
	private final static int DOWM = -1;
	private int state;

	public Sort(int state) {
		this.state = state;
	}

	public Sort() {

	}

	public int compare(String o1, String o2) {
		if (state == Sort.DOWM) {
			return sortDown(o1, o2);
		}
		return sortUp(o1, o2);
	}

	private int sortUp(String o1, String o2) {
		if (o1.compareTo(o2) < 0) {
			return -1;
		} else if (o1.compareTo(o2) > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	private int sortDown(String o1, String o2) {
		if (o1.compareTo(o2) > 0) {
			return -1;
		} else if (o1.compareTo(o2) < 0) {
			return 1;
		} else {
			return 0;
		}
	}

	public static void main(String[] args) {
		ArrayList<String> array = new ArrayList<String>();
		array.add("000001");
		array.add("000003");
		array.add("000005");
		array.add("000004");
		array.add("000002");
		Collections.sort(array, new Sort(Sort.DOWM));

		for (String str : array) {
			System.out.println(str);
		}
	}
}

  


功能:ArrayList中放置他是HashMap对象,这时要对其中的所有对象做排序。 

一、首先编写排序器 
DataComparator.java 
import Comparable.test1.Person; 
import java.util.Comparator; 
import java.util.HashMap; 
import java.text.Collator; 
import java.text.CollationKey; 

public class DataComparator 
	implements Comparator { 
	Collator collator = Collator.getInstance(); //调入这个是解决中文排序问题 
	public int compare(Object obj1, Object obj2) { 
	     HashMap map1 = (HashMap) obj1; 
	     HashMap map2 = (HashMap) obj2; 
	     String name1 = (String) map1.get("Name"); 
	     String name2 = (String) map2.get("Name"); 
	     CollationKey key1 = collator.getCollationKey(name1.toLowerCase()); 
	     CollationKey key2 = collator.getCollationKey(name2.toLowerCase()); 
	     return key1.compareTo(key2); 
	}
} 

  


二、测试 
ArrayListMapSortTest.java 
import java.util.Collections;
import Comparable.ArrayListMapSort.DataComparator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Date;
import java.io.File;

public class ArrayListMapSortTest {

	//列出文件夹下的文件及文件夹,不包括子文件夹中的文件和文件夹
	public ArrayList listFoldersAndFiles(String path) {
		File file = new File(path);
		File[] list = file.listFiles();
		ArrayList al = new ArrayList();
		for (int i = 0; i < list.length; i++) {
			//if (list[i].isDirectory())
			//listFolder(list[i].getPath());
		       HashMap hm = new HashMap();
		       hm.put("Name", list[i].getName());
		       hm.put("Path", list[i].getPath());
		       hm.put("AbsolutePath", list[i].getAbsolutePath());
		       hm.put("Parent", list[i].getParent());
		       //hm.put("isAbsolute", list[i].isAbsolute());
		       //hm.put("isDirectory", list[i].isDirectory());
		       //hm.put("isFile", list[i].isFile());
		       //hm.put("isHidden", list[i].isHidden());
		       hm.put("lastModified", new Date(list[i].lastModified()).toLocaleString());
		       al.add(hm);
		       //System.out.println(list[i].getName()+"    是文件夹:"+list[i].isDirectory());
		}
     return al;
   }

   public static void main(String[] args) {
	   ArrayListMapSortTest databean = new ArrayListMapSortTest();
	   ArrayList al = databean.listFoldersAndFiles("F:\\");
	   Collections.sort(al, new DataComparator());
	   for (int i = 0; i < al.size(); i++) {
		   HashMap hm = (HashMap) al.get(i);
		   for (Iterator iter = hm.entrySet().iterator(); iter.hasNext(); ) {
			   Map.Entry entry = (Map.Entry) iter.next();
			   Object key = entry.getKey();
			   Object val = entry.getValue();
			   System.out.print(key + "    " + val + "   ");
		   }
		   System.out.println();
	   }
	}
}