/// <summary>
/// Sort a entity array.
/// </summary>
/// <param name="orgArray">Entity array.</param>
/// <param name="isAsc">Sort order.</param>
/// <param name="orderBy">
/// An array of properties names. Must be a property in each of them,
/// and the properties comparable. Compare these properties values.
/// </param>
public static void Sort(object[] orgArray, bool isAsc, params string[] orderBy)
{
if (orgArray == null || orgArray.Length == 0)
return; sort(orgArray, 0, orgArray.Length, isAsc, orderBy);
} /// <summary>
/// Quicksort.
/// </summary>
/// <param name="orgArray">Entity array.</param>
/// <param name="low"></param>
/// <param name="high"></param>
/// <param name="orderBy">
/// An array of properties names. Must be a property in each of them,
/// and the properties comparable. Compare these properties values.
/// </param>
private static void sort(object[] orgArray, int low, int high, bool isAsc, params string[] orderBy)
{
int i, j, f;
object t;
if (low == high)
return;
if (low == high - 1)
{
if ((Compare(orgArray[low] , orgArray[high], orderBy) < 0) == isAsc)
{
t = orgArray[low];
orgArray[low] = orgArray[high];
orgArray[high] = t;
}
return;
} i = low;
j = high;
f = low;
while (i != j)
{
for (; j != i; j-- )
{
if ((Compare(orgArray[j], orgArray[f], orderBy) < 0) == isAsc)
{
t = orgArray[j];
orgArray[j] = orgArray[f];
orgArray[f] = t;
f = j;
break;
}
}
if (i == j) break;
for (; i != j; i++ )
{
if ((Compare(orgArray[i], orgArray[f], orderBy) < 0) == isAsc)
{
t = orgArray[i];
orgArray[i] = orgArray[f];
orgArray[f] = t;
f = i;
break;
}
}
}
if (f > low)
sort(orgArray, low, f - 1, isAsc, orderBy);
if ( f < high)
sort(orgArray, f + 1, high, isAsc, orderBy);
} /// <summary>
/// Compare two object.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="orderBy">
/// An array of properties names. Must be a property in each of them,
/// and the properties comparable. Compare these properties values.
/// </param>
/// <returns>
/// A 32-bit signed integer that indicates the relative order of the objects
/// being compared. The return value has these meanings: Value Meaning Less than
/// zero This instance is less than obj. Zero This instance is equal to obj.
/// Greater than zero This instance is greater than obj.
/// </returns>
private static int Compare(object a, object b, params string[] orderBy)
{
Type type = a.GetType(); PropertyInfo pro = null;
foreach (string proname in orderBy)
{
pro = type.GetProperty(proname);
if (!pro.GetValue(a, null).Equals(pro.GetValue(b, null)))
{
return ((IComparable)pro.GetValue(a, null)).CompareTo(pro.GetValue(b, null));
}
} return 0;
}
}
/// Sort a entity array.
/// </summary>
/// <param name="orgArray">Entity array.</param>
/// <param name="isAsc">Sort order.</param>
/// <param name="orderBy">
/// An array of properties names. Must be a property in each of them,
/// and the properties comparable. Compare these properties values.
/// </param>
public static void Sort(object[] orgArray, bool isAsc, params string[] orderBy)
{
if (orgArray == null || orgArray.Length == 0)
return; sort(orgArray, 0, orgArray.Length, isAsc, orderBy);
} /// <summary>
/// Quicksort.
/// </summary>
/// <param name="orgArray">Entity array.</param>
/// <param name="low"></param>
/// <param name="high"></param>
/// <param name="orderBy">
/// An array of properties names. Must be a property in each of them,
/// and the properties comparable. Compare these properties values.
/// </param>
private static void sort(object[] orgArray, int low, int high, bool isAsc, params string[] orderBy)
{
int i, j, f;
object t;
if (low == high)
return;
if (low == high - 1)
{
if ((Compare(orgArray[low] , orgArray[high], orderBy) < 0) == isAsc)
{
t = orgArray[low];
orgArray[low] = orgArray[high];
orgArray[high] = t;
}
return;
} i = low;
j = high;
f = low;
while (i != j)
{
for (; j != i; j-- )
{
if ((Compare(orgArray[j], orgArray[f], orderBy) < 0) == isAsc)
{
t = orgArray[j];
orgArray[j] = orgArray[f];
orgArray[f] = t;
f = j;
break;
}
}
if (i == j) break;
for (; i != j; i++ )
{
if ((Compare(orgArray[i], orgArray[f], orderBy) < 0) == isAsc)
{
t = orgArray[i];
orgArray[i] = orgArray[f];
orgArray[f] = t;
f = i;
break;
}
}
}
if (f > low)
sort(orgArray, low, f - 1, isAsc, orderBy);
if ( f < high)
sort(orgArray, f + 1, high, isAsc, orderBy);
} /// <summary>
/// Compare two object.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="orderBy">
/// An array of properties names. Must be a property in each of them,
/// and the properties comparable. Compare these properties values.
/// </param>
/// <returns>
/// A 32-bit signed integer that indicates the relative order of the objects
/// being compared. The return value has these meanings: Value Meaning Less than
/// zero This instance is less than obj. Zero This instance is equal to obj.
/// Greater than zero This instance is greater than obj.
/// </returns>
private static int Compare(object a, object b, params string[] orderBy)
{
Type type = a.GetType(); PropertyInfo pro = null;
foreach (string proname in orderBy)
{
pro = type.GetProperty(proname);
if (!pro.GetValue(a, null).Equals(pro.GetValue(b, null)))
{
return ((IComparable)pro.GetValue(a, null)).CompareTo(pro.GetValue(b, null));
}
} return 0;
}
}
解决方案 »
- ASP.NET WEB 服务应用程序中gridview如何才能显示图片
- SDK开发包怎么使
- 散分,感谢大家的帮助。这里对我的帮助太大了。谢谢大家。
- 屏幕右下角经常弹出的消息框,有腾讯的,有当地有关部门的,这个消息框功能如何实现的?比如我知道朋友的IP,我能给他发类似的消息框吗?
- 修改CheckBoxList 修改用户注册信息页面
- 一个很菜的问题,一直困扰我
- 链表与c#数据结构中LIST<T>
- 请问一个namespace中的各位form 之间如何共享变量
- 控制台 控制颜色, 不能用RGB 么?
- 怎么让Excel进程退出(在WinForm中)UP有分
- ****求高手分享,问一个小问题****
- C#网站开发
/// 给实体数组排序, 可以用依据多个属性的大小进行排序。
/// </summary>
/// <param name="orgArray">实体数组</param>
/// <param name="isAsc">升序降序</param>
/// <param name="orderBy">
/// 实体的属性名称数组。 只能是可以进行比较的属性。对这些属性的大小排序
/// </param>
public static void Sort(object[] orgArray, bool isAsc, params string[] orderBy)
{
if (orgArray == null || orgArray.Length == 0)
return; sort(orgArray, 0, orgArray.Length, isAsc, orderBy);
} /// <summary>
/// 快速排序法
/// </summary>
/// <param name="orgArray">实体数组</param>
/// <param name="low">排序开始下标</param>
/// <param name="high">排序结束下标</param>
/// <param name="orderBy">
/// 用来排序的属性。 只能是可以进行比较的属性。
/// </param>
private static void sort(object[] orgArray, int low, int high, bool isAsc, params string[] orderBy)
{
// 快速排序标志位 i是从前面向后扫描的下标位置 j从后向前 f为选定的参照值数组下标
int i, j, f;
// 交换位置用的临时变量
object t;
// 开始和结束上下标相等, 排序完成
if (low == high)
return;
// 只有二个元素排序,比较一次
if (low == high - 1)
{
// 比较的结果如果大于0 并且要求升序, 交换位置
// 比较的结果如果小于0 并且要求降序, 交换位置
if ((Compare(orgArray[low] , orgArray[high], orderBy) > 0) == isAsc)
{
t = orgArray[low];
orgArray[low] = orgArray[high];
orgArray[high] = t;
}
return;
} i = low;
j = high;
f = low; // 第一个数作为参照值 // 扫描前后标记重合, 排序结束
while (i != j)
{
// j从后向前扫描 每个值和参照值比较
for (; j != i; j-- )
{
// 比较的结果如果大于0 并且要求升序, 交换位置
// 比较的结果如果小于0 并且要求降序, 交换位置
if ((Compare(orgArray[j], orgArray[f], orderBy) > 0) == isAsc)
{
t = orgArray[j];
orgArray[j] = orgArray[f];
orgArray[f] = t;
f = j;
break;
}
}
// 是否扫描标记重合
if (i == j) break;
// i从前向后扫描
for (; i != j; i++ )
{
// 比较的结果如果大于0 并且要求升序, 交换位置
// 比较的结果如果小于0 并且要求降序, 交换位置
if ((Compare(orgArray[i], orgArray[f], orderBy) > 0) == isAsc)
{
t = orgArray[i];
orgArray[i] = orgArray[f];
orgArray[f] = t;
f = i;
break;
}
}
}
// 比参照值小的部分继续排序
if (f > low)
sort(orgArray, low, f - 1, isAsc, orderBy);
// 比参照值大的部分继续排序
if ( f < high)
sort(orgArray, f + 1, high, isAsc, orderBy);
} /// <summary>
/// 比较两实体大小
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="orderBy">
/// 实体的属性名称数组
/// </param>
/// <returns>
/// 0: a = b
/// > 0: a > b
/// < 0: a < b
/// </returns>
private static int Compare(object a, object b, params string[] orderBy)
{
Type type = a.GetType(); PropertyInfo pro = null;
foreach (string proname in orderBy)
{
pro = type.GetProperty(proname);
// a值和b值不相等,返回比较结果,相等比较下一属性
if (!pro.GetValue(a, null).Equals(pro.GetValue(b, null)))
{
return ((IComparable)pro.GetValue(a, null)).CompareTo(pro.GetValue(b, null));
}
}
// 所有属性相等, a = b
return 0;
}
}
改了一下, 还要加什么