public static bool IsSortable(Type enumType, out SortableAttribute.SortAlgorithm algorithm) { algorithm = SortableAttribute.SortAlgorithm.None; if (enumType.GetCustomAttributes(false).Length > 0) { foreach (object o in enumType.GetCustomAttributes(false)) { if (o.GetType() == typeof(SortableAttribute)) { algorithm = ((SortableAttribute)o).Algorithm; return(true); } } } return(false); }
private static IEnumerable <KeyValue> Sort(IEnumerable <KeyValue> collection, SortableAttribute.SortAlgorithm algoritm) { switch (algoritm) { case SortableAttribute.SortAlgorithm.AlphabeticallyOnly: return(collection.OrderBy(m => m.Key)); case SortableAttribute.SortAlgorithm.BySortValueAndThenAlphabetically: return(collection .OrderBy(n => n.Sortvalue ?? Convert.ToInt32(n.Value)) .ThenBy(n => n.Key)); case SortableAttribute.SortAlgorithm.BySortValue: return(collection.Select(m => new KeyValue() { Default = m.Default, Key = m.Key, Value = m.Value, Sortvalue = m.Sortvalue ?? Convert.ToInt32(m.Value), Groups = m.Groups }) .OrderBy(n => n.Sortvalue)); ; default: //SortableAttribute.SortAlgorithm.None return(collection); } }
public static bool IsSortable <T>(out SortableAttribute.SortAlgorithm algorithm) where T : System.Enum { return(IsSortable(typeof(T), out algorithm)); }