以某种方式订购的日期列表 [英] List of Dates ordered in a certain way

查看:88
本文介绍了以某种方式订购的日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期列表(格式为yyyyMM的字符串),如下所示:

I have a list of dates (strings in the format yyyyMM) as follows:

201608
201609
201610
201708
201709
201710

我想将它们重新排序如下:

I'd like to reorder them to be as follows:

201608
201708
201609
201709
201610
201710

我以为我可以颠倒字符串(即016102)来排序它们,但是显然,当月> = 10时,它会导致导致列表出现的问题,例如:

I thought I could reverse the strings (i.e. 016102) to order them but obviously with a month >= 10 it causes issues resulting in the list coming out like:

201610
201710
201608
201708
201609
201709

我尝试了以下操作: MyList.OrderBy(n => string.Concat(n.datestring.Reverse())).Select(n => n);

I tried the following: MyList.OrderBy(n => string.Concat(n.datestring.Reverse())).Select(n => n);

是否有一个很好的简洁方法(LINQ会很好)?感谢您的帮助.

Is there a nice succinct way of doing this (LINQ would be nice)? Thanks for any help.

推荐答案

使用LINQ,您可以按两位数字表示月份,然后按年份(和月份)排序:

Using LINQ, you order by the two digit month then the year (and month, but that shouldn't matter):

var ans = src.OrderBy(s => s.Substring(4)).ThenBy(s => s);

这篇关于以某种方式订购的日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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