c# - DateTimeFormatInfo.AbbreviatedMonthNames order -
i have list of string represents abbreviated months: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
if these values in random order, how can order them above: jan, feb, mar ...
?
list<string> months = new list<string>();
why don't use ordered list:
list<string> months = enumerable.range(1, 12) .select(m => datetimeformatinfo.invariantinfo.getabbreviatedmonthname(m)) .tolist();
list contains:
[0] "jan" [1] "feb" [2] "mar" [3] "apr" [4] "may" [5] "jun" [6] "jul" [7] "aug" [8] "sep" [9] "oct" [11] "dec"
if want month names in current language use:
datetimeformatinfo.currentinfo.getabbreviatedmonthname(m)
if have list contains month-names , list contains duplicates or not complete want order list naturally, can use indexof
:
monthlist = monthlist.orderby(s => months.indexof(s)).tolist();
here's approach doesn't need list compare uses datetime.parseexact
:
monthlist = monthlist .orderby(s => datetime.parseexact(s, "mmm", datetimeformatinfo.invariantinfo)) .tolist();
Comments
Post a Comment