c#如何对ArrayList排序?

puzhichen 2010-01-06 03:11:10
具体代码如下:

ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
var obj = new { a=i, b=i+1, c=i+2 };
list.Add(obj);
}

foreach (var m in list)
{
this.Response.Write(m);
}

要求:输出的新集合是对b进行排序了的!
...全文
1383 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaowang_11 2010-10-27
  • 打赏
  • 举报
回复
汗……害我多看了几眼7楼的代码
puzhichen 2010-01-06
  • 打赏
  • 举报
回复
汗,真对不住5楼,分子给错人了!
puzhichen 2010-01-06
  • 打赏
  • 举报
回复
谢谢楼上几位兄弟,问题解决!给分
l171147904 2010-01-06
  • 打赏
  • 举报
回复


class Person {
public String name;
public int id;
public Person (String name,int id) {
this.name=name;this.id=id;
}
..//other methods
}

List l=new ArrayList();
l.add(new Person("张三",1));
...//add some other Person object
Collections.sort(l,new Comparator () {
public int compare(Object o1,Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
return p1.name.compareTo(p2.name);
}
});
puzhichen 2010-01-06
  • 打赏
  • 举报
回复
加进去的数据是我自己填进去测试的,只是想给大家说清楚下集合结构而已!
Lovely_baby 2010-01-06
  • 打赏
  • 举报
回复

using System;
using System.Collections;

public class SamplesArrayList {

public class myReverserClass : IComparer {

// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}

}

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "QUICK" );
myAL.Add( "BROWN" );
myAL.Add( "FOX" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the default comparer.
myAL.Sort( 1, 3, null );
Console.WriteLine( "After sorting from index 1 to index 3 with the default comparer:" );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( 1, 3, myComparer );
Console.WriteLine( "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );

}

public static void PrintIndexAndValues( IEnumerable myList ) {

int i = 0;
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
Console.WriteLine();

}
}

转~
puzhichen 2010-01-06
  • 打赏
  • 举报
回复
不自己实现IComparable接口这种方法 ,有其他方法么。
huming_h 2010-01-06
  • 打赏
  • 举报
回复
加进去不是有序的吗?
liherun 2010-01-06
  • 打赏
  • 举报
回复
up
l171147904 2010-01-06
  • 打赏
  • 举报
回复
list.sort()

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧