简体   繁体   中英

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.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(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(); 
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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