如何排序这个内容? [英] How to sort this content?

查看:62
本文介绍了如何排序这个内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是LINQ的新人,我有一项任务可以对内容可能存储的内容进行排序。



Hi I am new one to LINQ, I have one task to sort the content the content may stored like this

list1(1909-1930,1921-1925,1907–21,1871–1924,2009-10,456–458,58–66)





如何排序如下





how to sort this like

58–66
456–458
1871–1924
1907–21
1909-1930
1921-1925





任何人给出解释如何解决



any one give explanation how to solve it

推荐答案

具体如何操作将取决于它们的存储方式。

如果这些是字符串,那么它有点复杂,但它仍然可能。假设你想要数字顺序,首先到1900年,然后到1930年:

Exactly how you do this will depend on how they are stored.
If those are strings, then it's a little complicated, but it is still possible. Assuming that you want numerical order, first by 1900, then by 1930:
List<string> list1 = new List<string>() {"1909-1930","1921-1925","1907-21","1871-1924","2009-10","456-458","58-66"};
list1 = list1.OrderBy(s => int.Parse(s.Substring(0, s.IndexOf('-'))))
             .ThenBy(s => int.Parse(s.Substring(s.IndexOf('-'))))
             .ToList();


这篇关于如何排序这个内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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