对实体数组排序, 使用快速排序法

依然白板 2010-05-17 05:08:54

/// <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 object[] Sort(object[] orgArray, bool isAsc, params string[] orderBy)
{
if (orgArray == null || orgArray.Length == 0)
return null;

sort(orgArray, 0, orgArray.Length, isAsc, orderBy);

return null;
}

/// <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;
}


我只是比较无聊
...全文
138 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayonglong 2010-05-20
  • 打赏
  • 举报
回复
lz在无私~
捷哥1999 2010-05-17
  • 打赏
  • 举报
回复
无聊中的分享,呵呵!
liuyu520hong 2010-05-17
  • 打赏
  • 举报
回复
多谢楼主分享!
gxingmin 2010-05-17
  • 打赏
  • 举报
回复
楼主在分享
happyboyxq1985 2010-05-17
  • 打赏
  • 举报
回复
应该是在分享。
mngzilin 2010-05-17
  • 打赏
  • 举报
回复
你是在分享?还是在提问?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧