排序列表< String>在C#中 [英] Sorting List<String> in C#

查看:71
本文介绍了排序列表< String>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据商品的整数值对列表进行排序

How to sort a List based on the item's integer value

列表就像

"1"
"5"
"3"
"6"
"11"
"9"
"NUM1"
"NUM0"

结果应该像

"1"
"3"
"5"
"6"
"9"
"11"
"NUM0"
"NUM1"

是否有使用LINQ或Lambda表达式执行此操作的想法?

is there any idea to do this using LINQ or Lambda expression?

预先感谢

推荐答案

怎么样:

    list.Sort((x, y) =>
    {
        int ix, iy;
        return int.TryParse(x, out ix) && int.TryParse(y, out iy)
              ? ix.CompareTo(iy) : string.Compare(x, y);
    });

这篇关于排序列表< String>在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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