java list 排序

//------------排序类型-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);
}

}

}
Java中,可以使用Collections类的sort方法对List进行排序。引用\[1\]中的代码展示了如何使用Collections.sort方法对一个包含整数的List进行排序。首先,创建一个ArrayList对象,并向其中添加整数元素。然后,使用Collections.sort方法对该List进行排序。最后,打印排序后的List。这种方法适用于简单的整数排序。 如果要对自定义对象进行排序,可以实现Comparable接口或使用Comparator接口。引用\[2\]中的代码展示了如何对一个包含自定义对象的List进行排序。首先,创建一个ArrayList对象,并向其中添加自定义对象。然后,使用Collections.sort方法对该List进行排序。最后,打印排序后的List。在这个例子中,自定义对象实现了Comparable接口,因此可以直接使用Collections.sort方法进行排序。 另一种方法是使用Listsort方法和Comparator接口。引用\[3\]中的代码展示了如何使用Listsort方法和Comparator接口对一个包含自定义对象的List进行排序。首先,创建一个ArrayList对象,并向其中添加自定义对象。然后,使用Listsort方法和Comparator.comparing方法对该List进行排序。最后,打印排序后的List。这种方法更简洁,只需要一行代码即可完成排序。 综上所述,可以根据需要选择合适的方法对Java中的List进行排序。 #### 引用[.reference_title] - *1* *3* [Java List排序的几种方式整理](https://blog.csdn.net/Wjhsmart/article/details/115935693)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Java:详解List集合的排序功能](https://blog.csdn.net/weixin_43092673/article/details/123910945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值