Skip to content

Instantly share code, notes, and snippets.

@gkinsman
Created February 15, 2012 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkinsman/1835185 to your computer and use it in GitHub Desktop.
Save gkinsman/1835185 to your computer and use it in GitHub Desktop.
Some guy in ##csharp asked for a solution to this...
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