如何对List 进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何对List 进行排序相关的知识,希望对你有一定的参考价值。

第一种:实体类实现IComparable接口,而且必须实现CompareTo方法
class Info:IComparable

public int Id get; set;
public string Name get; set;

public int CompareTo(object obj)
int result;
try

Info info = obj as Info;
if (this.Id > info.Id)

result = 0;

else
result = 1;
return result;

catch (Exception ex) throw new Exception(ex.Message);


调用方式如下,只需要用sort方法就能实现对list进行排序。
private static void ReadAccordingCompare()
List<Info> infoList = new List<Info>();
infoList.Add(
new Info() Id = 1, Name = "abc" );
infoList.Add(new Info() Id = 3, Name = "rose" );
infoList.Add(new Info() Id = 2, Name = "woft" );
infoList.Sort();
foreach (var item in infoList)

Console.WriteLine(item.Id + ":" + item.Name);


第二种方法:linq to list进行排序
运用linq实现对list排序,在实体类定义的时候就不需用实现IComparable接口,调用方式如下:
private static void ReadT(string str)
List<Info> infoList = new List<Info>();
infoList.Add(
new Info() Id = 1, Name = "woft" );
infoList.Add(new Info() Id=3,Name="rose");
infoList.Add(new Info() Id = 2, Name = "abc" );
Console.WriteLine("ReadT*********************");
IEnumerable<Info> query = null;
query = from items in infoList orderby items.Id select items;
foreach (var item in query)

Console.WriteLine(item.Id+":"+item.Name);


但是上面两种方式都只能对一个实体属性排序,如果对不同的属性排序的话只能写很多的if进行判断,这样显得很麻烦。且看下面的方式实现根据传入参数进行排序。
private static void ListSort(string field,string rule)

if (!string.IsNullOrEmpty(rule)&&(!rule.ToLower().Equals("desc")||!rule.ToLower().Equals("asc")))

try

List<Info> infoList = GetList();
infoList.Sort(
delegate(Info info1, Info info2)

Type t1 = info1.GetType();
Type t2 = info2.GetType();
PropertyInfo pro1 = t1.GetProperty(field);
PropertyInfo pro2 = t2.GetProperty(field);
return rule.ToLower().Equals("asc") ?
pro1.GetValue(info1, null).ToString().CompareTo(pro2.GetValue(info2, null).ToString()) :
pro2.GetValue(info2, null).ToString().CompareTo(pro1.GetValue(info1, null).ToString());
);
Console.WriteLine("*****ListSort**********");
foreach (var item in infoList)

Console.WriteLine(item.Id + "," + item.Name);


catch (Exception ex)

Console.WriteLine(ex.Message);

Console.WriteLine("ruls is wrong");


调用方式:
ListSort("Name","desc");//表示对Name进行desc排序
ListSort("Id","asc");//表示对Id进行asc排序。如此如果参数很多的话减少了很多判断。
参考技术A 我想你要的应该是这样的。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test_list_order
public static void main(String[] args)
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(4);
list.add(9);
list.add(3);
list.add(2);
list.add(11);
Collections.sort(list);
for (Integer i : list)
System.out.println(i);


list集合如何对里面的元素进行排序

Collections 是集合的公共类,提供各种工具,其中提供了排序方法。
Collections.sort(),方法两个参数,1,要排序的集合,2.排序方式
下面是匿名内部类,实现了排序借口,你也可以写外面。
Comparator c=new  Comparator<T>() {

@Override
public int compare(T o1, T o2) {
// TODO Auto-generated method stub
return 0;
}
}

 

以上是关于如何对List 进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何对List集合中的对象进行排序?

list如何排序 list排序方法是啥

如何对 List<Integer> 进行排序? [复制]

java怎么对list进行排序

list集合如何对里面的元素进行排序

如何对List集合中的对象进行按某个属性排序