Vector 多字段排序的Java实现
要求实现:
Vector 多字段排序,其中首元素不参与排序,第一二三字段升序,空排到前面。
//这里是Vector的元素定义 public class AVectorElement { private String sort_first_Field; private String sort_second_Field; private String sort_third_Field; /** * @return the sort_first_Field */ public String getSort_first_Field() { return sort_first_Field; } /** * @param sort_first_Field the sort_first_Field to set */ public void setSort_first_Field(String sort_first_Field) { this.sort_first_Field = sort_first_Field; } /** * @return the sort_second_Field */ public String getSort_second_Field() { return sort_second_Field; } /** * @param sort_second_Field the sort_second_Field to set */ public void setSort_second_Field(String sort_second_Field) { this.sort_second_Field = sort_second_Field; } /** * @return the sort_third_Field */ public String getSort_third_Field() { return sort_third_Field; } /** * @param sort_third_Field the sort_third_Field to set */ public void setSort_third_Field(String sort_third_Field) { this.sort_third_Field = sort_third_Field; } }
排序实现
package jp.co.hitachi.jkk.zaimu.player.kr; import java.util.Collections; import java.util.Comparator; import java.util.Vector; public class AVectorSort { /** * @param args */ public static void main(String[] args) { Vector<AVectorElement> vtElements = new Vector<AVectorElement>(); InstoVt(vtElements); // sort before print all elements System.out.println("Sort Before:"); pfVtAllElement(vtElements); AVectorElement bkE = vtElements.firstElement(); vtElements.removeElementAt(0); if (bkE == null) { System.out.println("null!"); return; } Collections.sort(vtElements, new Comparator<AVectorElement>() { public int compare(AVectorElement ev1, AVectorElement ev2) { String[][] arrStr = new String[2][3]; arrStr[0][0] = convertNullToEmpty(ev1.getSort_first_Field()); arrStr[1][0] = convertNullToEmpty(ev2.getSort_first_Field()); arrStr[0][1] = convertNullToEmpty(ev1.getSort_second_Field()); arrStr[1][1] = convertNullToEmpty(ev2.getSort_second_Field()); arrStr[0][2] = convertNullToEmpty(ev1.getSort_third_Field()); arrStr[1][2] = convertNullToEmpty(ev2.getSort_third_Field()); if (arrStr[0][0].compareTo(arrStr[1][0]) > 0) { return 1; } else if (arrStr[0][0].compareTo(arrStr[1][0]) < 0) { return -1; } else if (arrStr[0][1].compareTo(arrStr[1][1]) > 0) { return 1; } else if (arrStr[0][1].compareTo(arrStr[1][1]) < 0) { return -1; } else if (arrStr[0][2].compareTo(arrStr[1][2]) > 0) { return 1; } else if (arrStr[0][2].compareTo(arrStr[1][2]) < 0) { return -1; } return 0; } }); // add first element backuped before sort System.out.println("Sort After:"); vtElements.add(0, bkE); // sort after ,print all elements pfVtAllElement(vtElements); } private static void InstoVt(Vector<AVectorElement> vtElements) { AVectorElement e = null; // first element is exclude e = new AVectorElement(); e.setSort_first_Field("2029"); e.setSort_second_Field("1-070-000"); e.setSort_third_Field("1-070-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field("2001"); e.setSort_second_Field("1-070-000"); e.setSort_third_Field("1-070-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field("1001"); e.setSort_second_Field("1-070-000"); e.setSort_third_Field("1-070-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field("1003"); e.setSort_second_Field("1-070-000"); e.setSort_third_Field("1-070-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field("1002"); e.setSort_second_Field("1-080-000"); e.setSort_third_Field("1-070-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field("1002"); e.setSort_second_Field("1-080-000"); e.setSort_third_Field("1-060-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field("1002"); e.setSort_second_Field("1-070-000"); e.setSort_third_Field("1-070-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field(""); e.setSort_second_Field("1-080-000"); e.setSort_third_Field("1-060-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field(""); e.setSort_second_Field(""); e.setSort_third_Field("1-060-000"); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field(""); e.setSort_second_Field(""); e.setSort_third_Field(""); vtElements.add(e); e = new AVectorElement(); e.setSort_first_Field(""); e.setSort_second_Field("2-100-100"); e.setSort_third_Field(""); vtElements.add(e); } private static void pfVtAllElement(Vector<AVectorElement> vtElements) { for (AVectorElement e : vtElements) { System.out.println("Field1: " + e.getSort_first_Field() + ", Field2:" + e.getSort_second_Field() + ", Field1:" + e.getSort_third_Field() + "."); } } private static String convertNullToEmpty(String aString) { String ret = null; try { if (aString == null) { return ""; } return aString; } catch (Exception e) { ret = null; return ret; } } }
排序结果:
Sort Before: Field1: 2029, Field2:1-070-000, Field1:1-070-000. Field1: 2001, Field2:1-070-000, Field1:1-070-000. Field1: 1001, Field2:1-070-000, Field1:1-070-000. Field1: 1003, Field2:1-070-000, Field1:1-070-000. Field1: 1002, Field2:1-080-000, Field1:1-070-000. Field1: 1002, Field2:1-080-000, Field1:1-060-000. Field1: 1002, Field2:1-070-000, Field1:1-070-000. Field1: , Field2:1-080-000, Field1:1-060-000. Field1: , Field2:, Field1:1-060-000. Field1: , Field2:, Field1:. Field1: , Field2:2-100-100, Field1:. Sort After: Field1: 2029, Field2:1-070-000, Field1:1-070-000.(首元素不参与排序) Field1: , Field2:, Field1:. Field1: , Field2:, Field1:1-060-000. Field1: , Field2:1-080-000, Field1:1-060-000. Field1: , Field2:2-100-100, Field1:. Field1: 1001, Field2:1-070-000, Field1:1-070-000. Field1: 1002, Field2:1-070-000, Field1:1-070-000. Field1: 1002, Field2:1-080-000, Field1:1-060-000. Field1: 1002, Field2:1-080-000, Field1:1-070-000. Field1: 1003, Field2:1-070-000, Field1:1-070-000. Field1: 2001, Field2:1-070-000, Field1:1-070-000.