I have the following class for grouping
public class StringKeyGroup<T> : ObservableCollection<T>
{
public delegate string GetKeyDelegate(T item);
public string Key { get; private set; }
public StringKeyGroup(string key)
{
Key = key;
}
public static ObservableCollection<StringKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
{
var list = new ObservableCollection<StringKeyGroup<T>>();
foreach (var item in items)
{
var itemKey = getKey(item).ToLower();
var itemGroup = list.FirstOrDefault(li => li.Key == itemKey);
var itemGroupIndex = itemGroup != null ? list.IndexOf(itemGroup) : -1;
if (itemGroupIndex == -1)
{
list.Add(new StringKeyGroup<T>(itemKey));
itemGroupIndex = list.Count - 1;
}
if (itemGroupIndex >= 0 && itemGroupIndex < list.Count)
{
list[itemGroupIndex].Add(item);
}
}
if (sort)
{
string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
foreach (var group in list)
{
//group.ToList().Sort((c0, c1) => ci.CompareInfo.Compare(getKey(c0), getKey(c1)));
}
}
return list;
}
}
and this my data source.
public class Data {
public string Name {get;set;}
public string Month {get;set;}
}
the thing is the months is appear like : december 2013, june 2013 , january 2013
i want the data like january 2013, june 2013, december 2013 and so on. it depends on the month then the year Oldest to latest. I've been searching and i didn't find anything, please help.
I solved this. Instead of sorting the strings. i used integers
public int month.
and i already have a list of Data called collection for example.
i used the following
var OrderedData = collection.OrderBy( x => x.Month).ToList();
if you want to sort more than one property u can do the following
var OrderedData = collection.OrderBy( x => x.Month).ThenBy(y => y.YourPropertyName).ToList();