Created
February 15, 2012 11:29
-
-
Save gkinsman/1835185 to your computer and use it in GitHub Desktop.
Some guy in ##csharp asked for a solution to this...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
class Test { | |
static void Main() | |
{ | |
var items = new List<string>() { | |
"foo_20120212", | |
"foo_20120213", | |
"foo_20120214", | |
"foo_20120215", | |
"baz_20120212", | |
"baz_20120213", | |
"baz_20120214", | |
"baz_20120215"}; | |
var v = items.Select(c => new { Date = c.Split('_')[1], Str = c}) | |
.Select(g => new { Date = DateTime.ParseExact(g.Date,"yyyyMMdd",CultureInfo.InvariantCulture), Str = g.Str}) | |
.OrderByDescending(h => h.Date).ToList(); | |
v.ForEach(x => Console.WriteLine(String.Format("date: {0}, str: {1}",x.date,x.str))); | |
} | |
} | |
Output: | |
date: 15/02/2012 12:00:00 AM, str: foo_20120215 | |
date: 15/02/2012 12:00:00 AM, str: baz_20120215 | |
date: 14/02/2012 12:00:00 AM, str: foo_20120214 | |
date: 14/02/2012 12:00:00 AM, str: baz_20120214 | |
date: 13/02/2012 12:00:00 AM, str: foo_20120213 | |
date: 13/02/2012 12:00:00 AM, str: baz_20120213 | |
date: 12/02/2012 12:00:00 AM, str: foo_20120212 | |
date: 12/02/2012 12:00:00 AM, str: baz_20120212 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment