說明:
1、集合類型參數化;
2、可根據集合中的對象的各個屬性進行排序,傳入屬性名稱即可;
注:屬性必須實現了IComparable接口,C#中int、datetime、string等基本類型都已經實現了IComparable接口。
while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)
{
i++;
}
if (i < j)
{
list[j] = list[i];
j--;
}
list[i] = key;
}
else
{
while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)
{
j--;
}
if (i < j)
{
list[i] = list[j];
i++;
}
while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)
{
i++;
}
if (i < j)
{
list[j] = list[i];
j--;
}
list[i] = key;
}
}
//執行遞歸調用
QuickSort<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
}
}
}
/// <summary>
/// 排序類型
/// </summary>
public enum SortDirection
{
Ascending,
Descending
}
新聞熱點
疑難解答