導航:首頁 > 編程語言 > sortlistjava

sortlistjava

發布時間:2024-08-20 06:28:08

java 中 List<T>如何按照T中的一個欄位排序

可以通過以下工具類進行實現:
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* List按照指定欄位排序工具類
*
* @param <T>
*/
public class ListSortUtil<T> {
/**
* @param targetList 目標排序List
* @param sortField 排序欄位(實體類屬性名)
* @param sortMode 排序方式(asc or desc)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void sort(List<T> targetList, final String sortField, final String sortMode) {

Collections.sort(targetList, new Comparator() {
public int compare(Object obj1, Object obj2) {
int retVal = 0;
try {
//首字母轉大寫
String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w","");
String methodStr="get"+newStr;

Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);
Method method2 = ((T)obj2).getClass().getMethod(methodStr, null);
if (sortMode != null && "desc".equals(sortMode)) {
retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序
} else {
retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序
}
} catch (Exception e) {
throw new RuntimeException();
}
return retVal;
}
});
}

}

Collections.sort(list.);//升序

㈡ java 比較兩個list數組問題

我這有種解法:
1、將list1和list2進行合並,在合並過程中去重。
2、用Collections.sort()進行排序就行了。代碼如下:
List<Integer> list = new ArrayList<Integer>();
for (Integer i : list2) {
if (list.indexOf(i) == -1) {
list.add(i);
}
}
for (Integer i : list1) {
if (list.indexOf(i) == -1) {
list.add(i);
}
}
Collections.sort(list);

㈢ java 實現ArrayList的sort

在排序中,最重要的是自己實現自己的比較的行數,即是implements Comparator
實現方法 public int compare(Object o1, Object o2) 最為重要..

舉個例子:
package book.arrayset;

import java.util.Comparator;

/**
* 整數比較器,將整數按降序排列
*/
class MyIntComparator implements Comparator{

/**
* o1比o2大,返回-1;o1比o2小,返回1。
*/
public int compare(Object o1, Object o2) {
int i1 = ((Integer)o1).intValue();
int i2 = ((Integer)o2).intValue();
if (i1 < i2){
return 1;
}
if (i1 > i2){
return -1;
}
return 0;
}
}

//上面的為比較的函數實現,下面真正的添加數據,

//通過調用上面的比較函數實現自定義排序的功能

package book.arrayset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* 對List中的元素排序
*/
public class SortList {

public static void output(List list){
if (list == null){
return;
}
for (int i=0; i<list.size(); i++){
System.out.print(list.get(i).toString() + " ");
}
System.out.println();
}

public static void main(String[] args) {
List list = new ArrayList();
list.add(new Integer(5));
list.add(new Integer(8));
list.add(new Integer(1));
list.add(new Integer(3));
list.add(new Integer(2));
list.add(new Double(3.1));
System.out.println("list開始狀態");
SortList.output(list);
//Collections.sort方法將用默認比較器排列list的元素
Collections.sort(list);
System.out.println("list被默認比較器排序後的狀態");
SortList.output(list);
//下面將list的元素按降序排列
Collections.sort(list, new MyIntComparator());
System.out.println("list被自定義比較器排序後的狀態");
SortList.output(list);

//因此,對於任意自定義類的對象,當保存在集合類容器中後,如果需要對它們進行排序,
//需要自己提供適應於自定義類的比較器,自定義比較器必須實現Comparator介面。
//然後採用Collections.sort(list, comparator);方法對容器進行排序。
}
}

㈣ JAVA中如何使用SORT從大到小排.

在排序中,最重要的是自己實現自己的比較的行數,即是implements Comparator
實現方法 public int compare(Object o1, Object o2) 最為重要..

舉個例子:
package book.arrayset;

import java.util.Comparator;

/**
* 整數比較器,將整數按降序排列
*/
class MyIntComparator implements Comparator{

/**
* o1比o2大,返回-1;o1比o2小,返回1。
*/
public int compare(Object o1, Object o2) {
int i1 = ((Integer)o1).intValue();
int i2 = ((Integer)o2).intValue();
if (i1 < i2){
return 1;
}
if (i1 > i2){
return -1;
}
return 0;
}
}

//上面的為比較的函數實現,下面真正的添加數據,

//通過調用上面的比較函數實現自定義排序的功能

package book.arrayset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* 對List中的元素排序
*/
public class SortList {

public static void output(List list){
if (list == null){
return;
}
for (int i=0; i<list.size(); i++){
System.out.print(list.get(i).toString() + " ");
}
System.out.println();
}

public static void main(String[] args) {
List list = new ArrayList();
list.add(new Integer(5));
list.add(new Integer(8));
list.add(new Integer(1));
list.add(new Integer(3));
list.add(new Integer(2));
list.add(new Double(3.1));
System.out.println("list開始狀態");
SortList.output(list);
//Collections.sort方法將用默認比較器排列list的元素
Collections.sort(list);
System.out.println("list被默認比較器排序後的狀態");
SortList.output(list);
//下面將list的元素按降序排列
Collections.sort(list, new MyIntComparator());
System.out.println("list被自定義比較器排序後的狀態");
SortList.output(list);

//因此,對於任意自定義類的對象,當保存在集合類容器中後,如果需要對它們進行排序,
//需要自己提供適應於自定義類的比較器,自定義比較器必須實現Comparator介面。
//然後採用Collections.sort(list, comparator);方法對容器進行排序。
}
}

文件的讀取與輸出並排序 Java編程

參數裡面的isAscend 用來決定是升序排列 還是降序排列:
/***
* 按最後修改時間排序
* @param list
* @param isAscend
*/
public static void sortListByTime(List<FileInfo> list , boolean isAscend) {
// 對ListView中數據list排序
ComparatorByTime comparator = new ComparatorByTime(isAscend);
if (!list.isEmpty()) {
Log.e("sortListByTime()", "");
synchronized (list) {
Collections.sort(list, comparator);
}

}
}
/**
* 按文件大小排序
* @param list
*/
public static void sortListBySize(List<FileInfo> list , boolean isAscend) {
// 對ListView中數據list排序
ComparatorBySize comparator = new ComparatorBySize(isAscend);
if (!list.isEmpty()) {
Log.e("sortListBySize()", "");
synchronized (list) {
Collections.sort(list, comparator);
}

}
}
/**
* 按文件名稱排序
* @param list
*/
public static void sortListByName(List<FileInfo> list , boolean isAscend) {
// 對ListView中數據list排序
ComparatorByName comparator = new ComparatorByName(isAscend);
if (!list.isEmpty()) {
Log.e("sortListByName()", "");
synchronized (list) {
Collections.sort(list, comparator);
}

}
}

㈥ JAVA如何根據List中對象的屬性進行對象排序(江湖救急!)

最近這么多人問排序的問題啊,都快回答膩了,你可以先搜一搜看看別的網站有沒有,不過既然都回答了,我就估且再發些代碼吧,這里只用String的List排序,如果用到復雜的對象你自己懂得變通吧

publicclassSortList{
privatestaticDateFormatdf=newSimpleDateFormat("yyyy-MM-dd");

publicstaticvoidmain(String[]args){
List<String>dates=newArrayList<String>();
dates.add("2014-10-02");
dates.add("2013-12-01");
dates.add("2014-11-02");
dates.add("2014-01-05");

Collections.sort(dates,newComparator<String>(){
@Override
publicintcompare(Stringo1,Stringo2){
Dated1=null,d2=null;
try{
d1=df.parse(o1);
}catch(ParseExceptione){
e.printStackTrace();
}
try{
d2=df.parse(o2);
}catch(ParseExceptione){
e.printStackTrace();
}
if(null==d1||null==d2){
return0;
}
returnd1.compareTo(d2);
}
});
System.out.println(dates);
}

}
閱讀全文

與sortlistjava相關的資料

熱點內容
隱藏網路為什麼別人可以連我不行 瀏覽:355
超級終端發送文件超時 瀏覽:24
文件多少m有什麼意思 瀏覽:63
microsoftword2010 瀏覽:773
國務院通信大數據行程卡在哪裡 瀏覽:581
列印機和電腦數據線連好怎麼操作 瀏覽:877
iphone打開html文件 瀏覽:63
java操作excel之poi 瀏覽:700
java集合同步 瀏覽:861
軟工大數據軟考考什麼 瀏覽:190
受控體系文件的封面有什麼內容 瀏覽:778
夢幻錦衣是哪個文件夾 瀏覽:811
raid模式不能黑蘋果 瀏覽:859
如何優化網站推廣方案 瀏覽:40
編程怎麼入門去極客時間 瀏覽:504
查看oracle資料庫字元集編碼 瀏覽:658
pscc恢復文件 瀏覽:245
釘釘怎麼上網路直播課 瀏覽:595
怎麼用手機qq掃描文件在哪裡 瀏覽:17
微信瓶子怎麼提示用完了 瀏覽:288

友情鏈接