2016-11-29 46 views
-2

正如标题所示,我有一个价格列表,我希望排序。 我试着这样做:c#使用Linq排序小数列表

prices.Sort((a, b) => a - b); 

,但我得到一个错误:

Cannot implicitly convert type 'decimal' to 'int'. Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

我试图扩大这一个做类似

prices.Sort((a, b) { 
    var x = double.Parse(a); 
    var y = double.Parse(b); 
    return x - y; 
}); 

,但我得到了同样的错误。 有谁知道我怎么能轻松做到这一点?

+3

什么是价格列表 T是类型?你能给出一个清单的例子以及你想如何分类吗? – mybirthname

+3

看看你正试图呼叫的'List .Sort'超载的签名。你如何期待它的工作?我建议使用LINQ和'OrderBy'来代替,或者使用'decimal.Compare'作为比较...或者只是在没有任何参数的情况下调用'Sort'。 –

+1

这可能有助于http://stackoverflow.com/questions/5233179/sorting-a-generic-list-of-doubles – Fuzzybear

回答