Пример #1
0
        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);
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
 public static bool IsSortable <T>(out SortableAttribute.SortAlgorithm algorithm)
     where T : System.Enum
 {
     return(IsSortable(typeof(T), out algorithm));
 }