简体   繁体   English

在列表中排序月份和年份

[英]Sorting months and years in a list

I have a list of strings where each string is a month + year. 我有一个字符串列表,其中每个字符串都是一个月+年。 I found similar question here but It was list with months only. 我在这里找到了类似的问题但仅列出了几个月。 So my list looks like that 所以我的清单看起来像

List<string> monthsList = new List<string>();
       monthsList.Add("August 2015");
       monthsList.Add("June 2014");
       monthsList.Add("February 2014");
       monthsList.Add("June 2015");

Is there a way to order it like that? 有没有办法像这样订购?

February 2014
June 2014
June 2015
August 2015

Make sure that you change the DateTime.Parse format to so that it accounts for the year in addition to the month: 确保将DateTime.Parse格式更改为,以便它除了月份之外还考虑年份:

DateTime.ParseExact(x, "MMMM yyyy", CultureInfo.InvariantCulture)

And then make sure that you change the OrderBy key selector so that it considers the entire date and not only the month when performing the sort: 然后确保您更改了OrderBy键选择器,以便在执行排序时它不仅考虑月份而且还考虑整个日期:

.OrderBy(x => x.Sort)

The complete code will look like this: 完整的代码如下所示:

var sortedMonths = monthsList
    .Select(x => new { Name = x, Sort = DateTime.ParseExact(x, "MMMM yyyy", CultureInfo.InvariantCulture) })
    .OrderBy(x => x.Sort)
    .Select(x => x.Name)
    .ToArray();

This is something I built. 这是我建造的。 It works like a charm. 它像一种魅力。 See image for example of output. 有关输出示例,请参见图像。 月份和年份下拉列表

 public class MonthAndYear 
    {        
        private string _monthAndYear;

        public string Month { get; set; }
        public IDictionary<string, int> Months { get; set; }
        public int Year { get; set; }
        public string MonthYear
        {
            get
            {
                return string.Concat(Month," ", Year);
            }
            set
            {
                _monthAndYear = value;
            }
        }

        public MonthAndYear()
        {
            Months = new Dictionary<string, int>();
            Months.Add("January", 1);
            Months.Add("February", 2);
            Months.Add("March", 3);
            Months.Add("April", 4);
            Months.Add("May", 5);
            Months.Add("June", 6);
            Months.Add("July", 7);
            Months.Add("August", 8);
            Months.Add("September", 9);
            Months.Add("October", 10);
            Months.Add("November", 11);
            Months.Add("December", 12);
        }        

        public int MonthInteger(string month)
        {
            if (Months.ContainsKey(month))
            {
                return Months[month];
            }
            else
                return 1;
        }

        public List<MonthAndYear> SortMonthAndYear(List<string> _monthYearList)
        {           
            List<MonthAndYear> _sortedMonthAndYear = new List<MonthAndYear>();

            foreach (var _monthYear in _monthYearList)
            {
                _sortedMonthAndYear.Add
                (
                    new MonthAndYear
                    {
                        Month = _monthYear.Substring(0, _monthYear.IndexOf(' ')),
                        Year = Convert.ToInt32(_monthYear.Substring(Math.Max(0, _monthYear.Length - 4)))
                    }
                );
            }            
            return _sortedMonthAndYear.OrderBy(y => y.Year).ThenBy(m => m.MonthInteger(m.Month)).ToList(); 
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM