//------------排序类型-int、long、double、date、string
/**
* type
* @author boris
*
*/
public enum SortEntityType {
INT,LONG,DOUBLE,STRING,DATE
}
//---排序实体类--sortValue 存放非date类型的排序值;dateValue 存放date类型的排序值
import java.util.Date;
/**
* sort entry
* @author boris
*
* @param <T>
*/
public class SortEntity<T> {
private T t ;
private String sortValue;
private Date dateValue;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public String getSortValue() {
return sortValue;
}
public void setSortValue(String sortValue) {
this.sortValue = sortValue;
}
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
}
//排序工厂类
package com.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author boris
*
*/
public class SortEntityFactory {
public static List sortList(List<Map> list, boolean isAsc,String orderByField,SortEntityType sortEntityType){
if(orderByField == null || "".equals(orderByField.trim())){
return list;
}
List<SortEntity> sortEntityList = new ArrayList<SortEntity>();
if(sortEntityType == null){
for(Map map : list){
SortEntity sortEntry = new SortEntity();
String value = "";
Object objValue = map.get(orderByField);
if(objValue != null){
value = objValue.toString();
}
if(objValue instanceof Integer){
sortEntityType = SortEntityType.INT;
}else if(objValue instanceof Long){
sortEntityType = SortEntityType.LONG;
}else if(objValue instanceof Double){
sortEntityType = SortEntityType.DOUBLE;
}else if(objValue instanceof Date){
sortEntityType = SortEntityType.DATE;
Date dateValue = (Date) objValue;
sortEntry.setDateValue(dateValue);
}else if(objValue instanceof String){
sortEntityType = SortEntityType.STRING;
}
sortEntry.setSortValue(value);
sortEntry.setT(map);
sortEntityList.add(sortEntry);
}
}else{
for(Map map : list){
SortEntity sortEntity = new SortEntity();
Object objValue = map.get(orderByField);
String value = "";
if(objValue != null){
value = objValue.toString();
}
switch(sortEntityType){
case DATE:
if(objValue instanceof Date){
Date dateValue = (Date) objValue;
sortEntity.setDateValue(dateValue);
}else{
if(objValue != null){
Date dateValue = SmisUtil.getDate(value);
sortEntity.setDateValue(dateValue);
}
}
break;
default:
}
sortEntity.setSortValue(value);
sortEntity.setT(map);
sortEntityList.add(sortEntity);
}
}
List result = list;
try{
result = sortList(sortEntityList,isAsc,sortEntityType);
}catch(Exception e){
}
return result;
}
public static List sortList(List<SortEntity> sortEntryList,boolean isAsc,SortEntityType sortEntityType){
switch(sortEntityType){
case INT:
return sortIntList(sortEntryList,isAsc);
case LONG:
return sortLongList(sortEntryList,isAsc);
case DOUBLE:
return sortDoubleList(sortEntryList,isAsc);
case DATE:
return sortDateList(sortEntryList,isAsc);
default :
return sortStringList(sortEntryList,isAsc);
}
}
private static List sortStringList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
StrComparatorEntryAsc comparAsc=new StrComparatorEntryAsc();
Collections.sort(sortEntityList, comparAsc);
}else {
StrComparatorEntryDesc comparDesc=new StrComparatorEntryDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortIntList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
IntComparatorEntityAsc comparAsc=new IntComparatorEntityAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
IntComparatorEntityDesc comparDesc=new IntComparatorEntityDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortLongList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
LongComparatorAsc comparAsc=new LongComparatorAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
LongComparatorDesc comparDesc=new LongComparatorDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortDoubleList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
DoubleComparatorAsc comparAsc=new DoubleComparatorAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
DoubleComparatorDesc comparDesc=new DoubleComparatorDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortDateList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
DateComparatorEntryAsc comparAsc=new DateComparatorEntryAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
DateComparatorEntryDesc comparDesc=new DateComparatorEntryDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
}
class IntComparatorEntityAsc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
int valueInt1 = 0;
int valueInt2 = 0;
try{
valueInt1 = Integer.parseInt(sortValue1);
valueInt2 = Integer.parseInt(sortValue2);
}catch(Exception e){
e.getStackTrace();
}
if(valueInt1 < valueInt2){
return -1;
}else if(valueInt1 == valueInt2){
return 0;
}else{
return 1;
}
}
}
class IntComparatorEntityDesc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
int valueInt1 = 0;
int valueInt2 = 0;
try{
valueInt1 = Integer.parseInt(sortValue1);
valueInt2 = Integer.parseInt(sortValue2);
}catch(Exception e){
e.getStackTrace();
}
if(valueInt1 > valueInt2){
return -1;
}else if(valueInt1 == valueInt2){
return 0;
}else{
return 1;
}
}
}
class LongComparatorAsc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
long longValue1 = 0L;
long longValue2 = 0L;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
longValue1 = Long.parseLong(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
longValue2 = Long.parseLong(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(longValue1 > longValue2){
flag = 1;
}else if(longValue1 == longValue2){
flag = 0;
}
return flag;
}
}
class LongComparatorDesc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
long longValue1 = 0L;
long longValue2 = 0L;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
longValue1 = Long.parseLong(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
longValue2 = Long.parseLong(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(longValue1 < longValue2){
flag = 1;
}else if(longValue1 == longValue2){
flag = 0;
}
return flag;
}
}
class DoubleComparatorAsc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
double doubleValue1 = 0.0;
double doubleValue2 = 0.0;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
doubleValue1 = Double.parseDouble(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
doubleValue2 = Double.parseDouble(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(doubleValue1 > doubleValue2){
flag = 1;
}else if(doubleValue1 == doubleValue2){
flag = 0;
}
return flag;
}
}
class DoubleComparatorDesc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
double doubleValue1 = 0.0;
double doubleValue2 = 0.0;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
doubleValue1 = Double.parseDouble(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
doubleValue2 = Double.parseDouble(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(doubleValue1 < doubleValue2){
flag = 1;
}else if(doubleValue1 == doubleValue2){
flag = 0;
}
return flag;
}
}
class DateComparatorEntryAsc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
Date dateValue1 = new Date();
Date dateValue2 = new Date();
if(sortEntity1 != null && sortEntity1.getDateValue() != null){
dateValue1 = sortEntity1.getDateValue();
}
if(sortEntity2 != null && sortEntity2.getDateValue() != null){
dateValue2 = sortEntity2.getDateValue();
}
return dateValue1.compareTo(dateValue2);
}
}
class DateComparatorEntryDesc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
Date dateValue1 = new Date();
Date dateValue2 = new Date();
if(sortEntity1 != null && sortEntity1.getDateValue() != null){
dateValue1 = sortEntity1.getDateValue();
}
if(sortEntity2 != null && sortEntity2.getDateValue() != null){
dateValue2 = sortEntity2.getDateValue();
}
return dateValue2.compareTo(dateValue1);
}
}
class StrComparatorEntryAsc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
return sortValue2.compareTo(sortValue1);
}
}
class StrComparatorEntryDesc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
return sortValue1.compareTo(sortValue2);
}
}
//--测试类
package com.sort;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Sorttest {
private static Date getDateStr(String dateStr){
SimpleDateFormat dateFormate = new SimpleDateFormat("MM/dd/yyyy");
ParsePosition parsePositon = new ParsePosition(0);
Date date = dateFormate.parse(dateStr, parsePositon);
return date;
}
/**
* @param args
*/
public static void main(String[] args) {
boolean isAsc = true;
String keyStr = "keyStr";
String keyInt = "keyInt";
String keyLong = "keyLong";
String keyDouble = "keyDouble";
String keyDate = "keyDate";
Map map1 = new HashMap();
map1.put(keyStr, "aaa");
map1.put(keyInt, 14);
map1.put(keyLong, 14L);
map1.put(keyDouble, 14.11);
map1.put(keyDate, getDateStr("11/5/2013"));
map1.put(keyDate, "11/5/2013");
Map map2 = new HashMap();
map2.put(keyStr, "abb");
map2.put(keyInt, 5);
map2.put(keyLong, 5L);
map2.put(keyDouble,51.11 );
map2.put(keyDate,getDateStr("5/7/2014"));
map2.put(keyDate,"5/7/2014");
Map map3 = new HashMap();
map3.put(keyStr, "acc");
map3.put(keyInt, 210);
map3.put(keyLong, 210L);
map3.put(keyDouble, 33.11);
map3.put(keyDate,getDateStr("12/08/2013"));
map3.put(keyDate,"12/08/2013");
Map map4 = new HashMap();
map4.put(keyStr, "add");
map4.put(keyInt, 7);
map4.put(keyLong, 7L);
map4.put(keyDouble, 7.1);
map4.put(keyDate,getDateStr("10/11/2012"));
map4.put(keyDate,"10/11/2012");
Map map5 = new HashMap();
map5.put(keyStr, "aee");
map5.put(keyInt, 10);
map5.put(keyLong, 10L);
map5.put(keyDouble, 80.00);
map5.put(keyDate,getDateStr("11/22/2013"));
map5.put(keyDate,"11/22/2013");
List<Map> list = new ArrayList<Map>();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
List<Map> sortList = SortEntityFactory.sortList(list, false ,"ee", null);
for(Map map : sortList){
System.out.println(map);
}
}
}
/**
* type
* @author boris
*
*/
public enum SortEntityType {
INT,LONG,DOUBLE,STRING,DATE
}
//---排序实体类--sortValue 存放非date类型的排序值;dateValue 存放date类型的排序值
import java.util.Date;
/**
* sort entry
* @author boris
*
* @param <T>
*/
public class SortEntity<T> {
private T t ;
private String sortValue;
private Date dateValue;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public String getSortValue() {
return sortValue;
}
public void setSortValue(String sortValue) {
this.sortValue = sortValue;
}
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
}
//排序工厂类
package com.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author boris
*
*/
public class SortEntityFactory {
public static List sortList(List<Map> list, boolean isAsc,String orderByField,SortEntityType sortEntityType){
if(orderByField == null || "".equals(orderByField.trim())){
return list;
}
List<SortEntity> sortEntityList = new ArrayList<SortEntity>();
if(sortEntityType == null){
for(Map map : list){
SortEntity sortEntry = new SortEntity();
String value = "";
Object objValue = map.get(orderByField);
if(objValue != null){
value = objValue.toString();
}
if(objValue instanceof Integer){
sortEntityType = SortEntityType.INT;
}else if(objValue instanceof Long){
sortEntityType = SortEntityType.LONG;
}else if(objValue instanceof Double){
sortEntityType = SortEntityType.DOUBLE;
}else if(objValue instanceof Date){
sortEntityType = SortEntityType.DATE;
Date dateValue = (Date) objValue;
sortEntry.setDateValue(dateValue);
}else if(objValue instanceof String){
sortEntityType = SortEntityType.STRING;
}
sortEntry.setSortValue(value);
sortEntry.setT(map);
sortEntityList.add(sortEntry);
}
}else{
for(Map map : list){
SortEntity sortEntity = new SortEntity();
Object objValue = map.get(orderByField);
String value = "";
if(objValue != null){
value = objValue.toString();
}
switch(sortEntityType){
case DATE:
if(objValue instanceof Date){
Date dateValue = (Date) objValue;
sortEntity.setDateValue(dateValue);
}else{
if(objValue != null){
Date dateValue = SmisUtil.getDate(value);
sortEntity.setDateValue(dateValue);
}
}
break;
default:
}
sortEntity.setSortValue(value);
sortEntity.setT(map);
sortEntityList.add(sortEntity);
}
}
List result = list;
try{
result = sortList(sortEntityList,isAsc,sortEntityType);
}catch(Exception e){
}
return result;
}
public static List sortList(List<SortEntity> sortEntryList,boolean isAsc,SortEntityType sortEntityType){
switch(sortEntityType){
case INT:
return sortIntList(sortEntryList,isAsc);
case LONG:
return sortLongList(sortEntryList,isAsc);
case DOUBLE:
return sortDoubleList(sortEntryList,isAsc);
case DATE:
return sortDateList(sortEntryList,isAsc);
default :
return sortStringList(sortEntryList,isAsc);
}
}
private static List sortStringList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
StrComparatorEntryAsc comparAsc=new StrComparatorEntryAsc();
Collections.sort(sortEntityList, comparAsc);
}else {
StrComparatorEntryDesc comparDesc=new StrComparatorEntryDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortIntList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
IntComparatorEntityAsc comparAsc=new IntComparatorEntityAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
IntComparatorEntityDesc comparDesc=new IntComparatorEntityDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortLongList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
LongComparatorAsc comparAsc=new LongComparatorAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
LongComparatorDesc comparDesc=new LongComparatorDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortDoubleList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
DoubleComparatorAsc comparAsc=new DoubleComparatorAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
DoubleComparatorDesc comparDesc=new DoubleComparatorDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
private static List sortDateList(List<SortEntity> sortEntityList,boolean isAsc){
List list = new ArrayList();
if( isAsc){
DateComparatorEntryAsc comparAsc=new DateComparatorEntryAsc();
Collections.sort(sortEntityList, comparAsc);
}else{
DateComparatorEntryDesc comparDesc=new DateComparatorEntryDesc();
Collections.sort(sortEntityList, comparDesc);
}
for(SortEntity sortEntry : sortEntityList){
list.add(sortEntry.getT());
}
return list;
}
}
class IntComparatorEntityAsc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
int valueInt1 = 0;
int valueInt2 = 0;
try{
valueInt1 = Integer.parseInt(sortValue1);
valueInt2 = Integer.parseInt(sortValue2);
}catch(Exception e){
e.getStackTrace();
}
if(valueInt1 < valueInt2){
return -1;
}else if(valueInt1 == valueInt2){
return 0;
}else{
return 1;
}
}
}
class IntComparatorEntityDesc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
int valueInt1 = 0;
int valueInt2 = 0;
try{
valueInt1 = Integer.parseInt(sortValue1);
valueInt2 = Integer.parseInt(sortValue2);
}catch(Exception e){
e.getStackTrace();
}
if(valueInt1 > valueInt2){
return -1;
}else if(valueInt1 == valueInt2){
return 0;
}else{
return 1;
}
}
}
class LongComparatorAsc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
long longValue1 = 0L;
long longValue2 = 0L;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
longValue1 = Long.parseLong(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
longValue2 = Long.parseLong(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(longValue1 > longValue2){
flag = 1;
}else if(longValue1 == longValue2){
flag = 0;
}
return flag;
}
}
class LongComparatorDesc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
long longValue1 = 0L;
long longValue2 = 0L;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
longValue1 = Long.parseLong(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
longValue2 = Long.parseLong(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(longValue1 < longValue2){
flag = 1;
}else if(longValue1 == longValue2){
flag = 0;
}
return flag;
}
}
class DoubleComparatorAsc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
double doubleValue1 = 0.0;
double doubleValue2 = 0.0;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
doubleValue1 = Double.parseDouble(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
doubleValue2 = Double.parseDouble(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(doubleValue1 > doubleValue2){
flag = 1;
}else if(doubleValue1 == doubleValue2){
flag = 0;
}
return flag;
}
}
class DoubleComparatorDesc implements Comparator<SortEntity>{
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
double doubleValue1 = 0.0;
double doubleValue2 = 0.0;
try{
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
doubleValue1 = Double.parseDouble(sortEntity1.getSortValue());
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
doubleValue2 = Double.parseDouble(sortEntity2.getSortValue());
}
}catch(NumberFormatException e){
e.getStackTrace();
}
int flag = -1;
if(doubleValue1 < doubleValue2){
flag = 1;
}else if(doubleValue1 == doubleValue2){
flag = 0;
}
return flag;
}
}
class DateComparatorEntryAsc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
Date dateValue1 = new Date();
Date dateValue2 = new Date();
if(sortEntity1 != null && sortEntity1.getDateValue() != null){
dateValue1 = sortEntity1.getDateValue();
}
if(sortEntity2 != null && sortEntity2.getDateValue() != null){
dateValue2 = sortEntity2.getDateValue();
}
return dateValue1.compareTo(dateValue2);
}
}
class DateComparatorEntryDesc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
Date dateValue1 = new Date();
Date dateValue2 = new Date();
if(sortEntity1 != null && sortEntity1.getDateValue() != null){
dateValue1 = sortEntity1.getDateValue();
}
if(sortEntity2 != null && sortEntity2.getDateValue() != null){
dateValue2 = sortEntity2.getDateValue();
}
return dateValue2.compareTo(dateValue1);
}
}
class StrComparatorEntryAsc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
return sortValue2.compareTo(sortValue1);
}
}
class StrComparatorEntryDesc implements Comparator<SortEntity> {
public int compare(SortEntity sortEntity1, SortEntity sortEntity2) {
String sortValue1 = "";
String sortValue2 = "";
if(sortEntity1 != null && sortEntity1.getSortValue() != null){
sortValue1 = sortEntity1.getSortValue();
}
if(sortEntity2 != null && sortEntity2.getSortValue() != null){
sortValue2 = sortEntity2.getSortValue();
}
return sortValue1.compareTo(sortValue2);
}
}
//--测试类
package com.sort;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Sorttest {
private static Date getDateStr(String dateStr){
SimpleDateFormat dateFormate = new SimpleDateFormat("MM/dd/yyyy");
ParsePosition parsePositon = new ParsePosition(0);
Date date = dateFormate.parse(dateStr, parsePositon);
return date;
}
/**
* @param args
*/
public static void main(String[] args) {
boolean isAsc = true;
String keyStr = "keyStr";
String keyInt = "keyInt";
String keyLong = "keyLong";
String keyDouble = "keyDouble";
String keyDate = "keyDate";
Map map1 = new HashMap();
map1.put(keyStr, "aaa");
map1.put(keyInt, 14);
map1.put(keyLong, 14L);
map1.put(keyDouble, 14.11);
map1.put(keyDate, getDateStr("11/5/2013"));
map1.put(keyDate, "11/5/2013");
Map map2 = new HashMap();
map2.put(keyStr, "abb");
map2.put(keyInt, 5);
map2.put(keyLong, 5L);
map2.put(keyDouble,51.11 );
map2.put(keyDate,getDateStr("5/7/2014"));
map2.put(keyDate,"5/7/2014");
Map map3 = new HashMap();
map3.put(keyStr, "acc");
map3.put(keyInt, 210);
map3.put(keyLong, 210L);
map3.put(keyDouble, 33.11);
map3.put(keyDate,getDateStr("12/08/2013"));
map3.put(keyDate,"12/08/2013");
Map map4 = new HashMap();
map4.put(keyStr, "add");
map4.put(keyInt, 7);
map4.put(keyLong, 7L);
map4.put(keyDouble, 7.1);
map4.put(keyDate,getDateStr("10/11/2012"));
map4.put(keyDate,"10/11/2012");
Map map5 = new HashMap();
map5.put(keyStr, "aee");
map5.put(keyInt, 10);
map5.put(keyLong, 10L);
map5.put(keyDouble, 80.00);
map5.put(keyDate,getDateStr("11/22/2013"));
map5.put(keyDate,"11/22/2013");
List<Map> list = new ArrayList<Map>();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
List<Map> sortList = SortEntityFactory.sortList(list, false ,"ee", null);
for(Map map : sortList){
System.out.println(map);
}
}
}