如何使用Lambda表达式对对象中的INTEGERS进行排序? [英] How do I use a Lambda expression to sort INTEGERS inside a object?

查看:127
本文介绍了如何使用Lambda表达式对对象中的INTEGERS进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象集合,我知道我可以按NAME(字符串类型)通过

  collEquipment进行排序。 Sort((x,y)=> string.Compare(x.ItemName,y.ItemName)); 



$ b

按照ID(整数类型)排序,没有Int32.Compare这样的东西



那么我该怎么做呢?这不起作用

  collEquipment.Sort((x,y)=>(x.ID< y.ID )); //错误

我知道答案会很简单。 Lambda表达式让我困惑。

解决方案

试试这个

  collEquipment.Sort((x,y)=> y.ID-x.ID); 


I have a collection of objects and I know that I can sort by NAME (string type) by saying

collEquipment.Sort((x, y) => string.Compare(x.ItemName, y.ItemName));

that WORKS.

But I want to sort by a ID (integer type) and there is no such thing as Int32.Compare

So how do I do this? This doesn't work

collEquipment.Sort((x, y) => (x.ID < y.ID));  //error

I know the answer is going to be really simple. Lambda expressions confuse me.

解决方案

try this

collEquipment.Sort((x, y) => y.ID - x.ID);

这篇关于如何使用Lambda表达式对对象中的INTEGERS进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆