void SetPropertyDescription(int i, string name, ListSortDirection direction) { sortDescriptions[i] = new DynamicSortDescription() { Direction = direction, NameChain = name.Split('.') }; List <PropertyInfo> props = new List <PropertyInfo>(); Type type = typeof(T); for (int j = 0; j < sortDescriptions[i].NameChain.Length; ++j) { PropertyInfo prop = type.GetProperty(sortDescriptions[i].NameChain[j]); if (prop == null) { break; } props.Add(prop); type = prop.PropertyType; } sortDescriptions[i].PropertyChain = props.ToArray(); }
int AscendingCompare(DynamicSortDescription sort_description, T x, T y) { //initially left and right are objects of type derived from T object left = x; object right = y; for (int j = 0; j < sort_description.NameChain.Length; ++j) { PropertyInfo left_prop; PropertyInfo right_prop; if (j < sort_description.PropertyChain.Length) { PropertyInfo prop = sort_description.PropertyChain[j]; left_prop = prop; right_prop = prop; } else { string prop_name = sort_description.NameChain[j]; left_prop = left.GetType().GetProperty(prop_name); right_prop = right.GetType().GetProperty(prop_name); } left = left_prop != null?left_prop.GetValue(left, null) : null; right = right_prop != null?right_prop.GetValue(right, null) : null; if (left == null || right == null) { if (left == null && right == null) { return(0); } if (left == null) { return(-1); //nulls goes first } return(1); } } IComparable left_comp = left as IComparable; return(left_comp.CompareTo(right)); }