class Program
{
static void Main(string[] args)
{
//Period 01 2008, Period 02 2008
Series s1 = new Series { Text = "Period 01 2008", Value = "1"};
Series s2 = new Series { Text = "Period 02 2008", Value = "2" };
Series s3 = new Series { Text = "Period 03 2008", Value = "3" };
Series s11 = new Series { Text = "Period 01 2009", Value = "1" };
Series s21 = new Series { Text = "Period 02 2009", Value = "2" };
Series s31 = new Series { Text = "Period 03 2009", Value = "3" };
Series s12 = new Series { Text = "Period 01 2010", Value = "1" };
Series s22 = new Series { Text = "Period 02 2010", Value = "2" };
Series s32 = new Series { Text = "Period 03 2010", Value = "3" };
List<Series> series = new List<Series> { s21, s31, s1, s12, s11, s2, s22, s3, s32 };
// mySeries.ForEach(i => Console.WriteLine("Period {0} {1}",i.Period, i.Year));
series.ForEach(i => Console.WriteLine(i.Text));
Console.WriteLine("************************");
series.OrderBy(i => i.Text);
series.ForEach(i=> Console.WriteLine(i.Text));
}
}
public class Series
{
public string Text { get; set; }
public string Value {get;set;}
}
Basically I want my list to be ordered by the Text property of the Series class. I have a work-around by splitting the text, but I am looking for any other way – using IComparer maybe?
The ordered list should be something like this:
Period 01 2008, Period 02 2008, Period 03 2008, Period 01 2009,
Period 02 2009…
Update: the definition of the Series class cannot be changed.
Add following comparer
And change last two lines in your example as below