ArrayList如何按两种条件排序

kimo4ask 2009-07-07 10:06:09
ArrayList里存放的对象有2个属性,一个Type1,一个Type2,想对整个ArrayList按Type1先排序,然后在Type1排序好的基础上再安Type2排序。

比如说:

排序前可能是这样的情况
Type1 Type2
--------------
1 a
2 c
2 a
1 b
2 b

希望排序后成为这样的情况
Type1 Type2
--------------
1 a
1 b
2 a
2 b
2 c

我现在只知道按Type1排序的方法,就是实现IComparer的那种,请问如何按2种条件排序?
...全文
747 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixianggao 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 paulin 的回复:]
通过arrlyList的Sort(IComparer)
存放的对象类继承IComparer 接口
实现Compare方法时先比较Type1,如果Type1相等继续比较Type2
[/Quote]
受到 13 楼滴启发,按把原有滴 DataModelManager<T>.Sort(List<T> listOfT, string sortPropName)
改为了
DataModelManager<T>.Sort(List<T> listOfT, List<SortInfo<T>> sortInfoList)

SortInfo 是用于保存 排序属性名及升降序滴对象,用 List 来实现多属性同时排序;

例如:
DataModelManager<Student>.Sort(studentList, Student.School_Symbol, Student.Class_Symbol, Student.Name_Symbol);
这是一个 Sort 的简单重载,将 studentList 按照 学生的 学校名、班级、姓名 排序。

刚刚测试了,多属性排序工作正常!
paulin 2009-07-08
  • 打赏
  • 举报
回复
通过arrlyList的Sort(IComparer)
存放的对象类继承IComparer 接口
实现Compare方法时先比较Type1,如果Type1相等继续比较Type2
class Class1:IComparer
{
private int id;
private string name;
public Class1()
{
}

public Class1(int id, string name)
{
this.id = id;
this.name = name;
}

int IComparer.Compare(Object x, Object y)
{
Class1 tempx = x as Class1;
Class1 tempy = y as Class1;
int returnCode=-2;
if (tempx != null && tempy != null)
{
returnCode = tempx.id.CompareTo(tempy.id);
if (returnCode == 0)
{
returnCode = tempx.name.CompareTo(tempy.name);
}
}

return returnCode;
}
}

ArrayList arrlist = new ArrayList();
arrlist.Add(new Class1(1, "a"));
arrlist.Add(new Class1(2, "c"));
arrlist.Add(new Class1(2, "a"));
arrlist.Add(new Class1(1, "b"));
arrlist.Add(new Class1(2, "b"));

IComparer myComparer = new Class1();
arrlist.Sort(myComparer);
破碎的脸 2009-07-08
  • 打赏
  • 举报
回复
from l in list orderby l.Type1, l.Type2 select l;

晕。抢我代码。。。。。。。。。对,没错,用LINQ,前面的仁兄已经给了最好答案了。

kimo4ask 2009-07-07
  • 打赏
  • 举报
回复
2.0里如何实现?有类似的方法吗?
kimo4ask 2009-07-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lqknife 的回复:]
arrayList.Cast <T>.Orderby(T.T1).Thenby(T.T2)
如果你的类型是基础类型属性的话并且我的方法没拼错那就直接可以使用了O(∩_∩)O~
[/Quote]

这个也写法没见过阿~也是3.5的新语法吗?
kimo4ask 2009-07-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 oec2003 的回复:]
var a= from lin list
orderby l.Type1, l.Type2
select l;
[/Quote]

这个写法没见过阿~是3.5的新语法吗?
落伍者 2009-07-07
  • 打赏
  • 举报
回复
http://xujiayou.w66.mydnns.cn/post/5.html
落伍者 2009-07-07
  • 打赏
  • 举报
回复
自己写比较函数。例子见我的个人网站。
【签名】每个月总有那么几天,您的网络会受到黑客的攻击?坐立不安,烦躁无力,使用我和human开发的“月月舒”防火墙,超轻超薄,易于携带,提供由内到外的全方位保护,即使流量再大,也可以冲浪自如,再也不用担心侧漏啦。
http://webtrados.008.net 网络小筑,常来坐坐。
LQknife 2009-07-07
  • 打赏
  • 举报
回复
arrayList.Cast<T>.Orderby(T.T1).Thenby(T.T2)
如果你的类型是基础类型属性的话并且我的方法没拼错那就直接可以使用了O(∩_∩)O~
zhaoweiting0609 2009-07-07
  • 打赏
  • 举报
回复
关注一下
oec2003 2009-07-07
  • 打赏
  • 举报
回复
可以 用List来实现


public class TempType
{
public int Type1 { get; set; }
public string Type2 { get; set; }

public TempType(int type1, string type2)
{
Type1 = type1;
Type2 = type2;
}
}

List<TempType> list = new List<TempType>();
list.Add(new TempType(1, "a"));
list.Add(new TempType(2, "c"));
list.Add(new TempType(2, "a"));
list.Add(new TempType(1, "b"));
list.Add(new TempType(2, "b"));
var a = from l in list
orderby l.Type1, l.Type2
select l;
jasondct 2009-07-07
  • 打赏
  • 举报
回复
可以的 ,先把Type1 为1的值读出来 在判断Type2 值小就放在前面。
Type1 为2的值也一样。
如果你有非常多的数据 , 就先把Type1值排序读出,然后循环Type1 取出值,然后排Type2
十八道胡同 2009-07-07
  • 打赏
  • 举报
回复
用sort,排序比较函数自己写
yixianggao 2009-07-07
  • 打赏
  • 举报
回复
呵呵,俺最近也在考虑类似滴问题,不过是在 List<T> 上实现多属性排序,
可是手头的项目正在重构中,没工夫弄这个,先说说基本思路:

1 按 Type1 正常排序;
2 取出 Type1 唯一值,即不重复值,
3 遍历 Type1 唯一值,从对排序后的列表中取出相应集合,再按 Type2 排序即可!
4 收工!

扩展,如果还有 Type3 排序,就重复 2、3 两步即可!

嘿嘿,说着说着基本都实现了,俺明早起来去Coding出来!
jiedamu 2009-07-07
  • 打赏
  • 举报
回复
不能这样排序的。sort功能有限

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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