对实体数组排序, 使用快速排序法
/// <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;
}
我只是比较无聊
...全文
相关推荐
使用快速排序法对一维数组进行排序
描述中的程序实现了快速排序法,可能是用一种编程语言如C++、Java或Python编写的,用于对一维数组进行排序。
快速排序法C语言详解
快速排序是一种高效的排序算法,它使用分治策略来把一个序列分为较小和较大的两个子序列,然后递归地排序两个子序列。在C语言中实现快速排序,通常涉及到以下几个关键知识点:1.
C#使用sort方法对数组进行快速排序
本篇文章将深入探讨如何使用`Sort`方法对数组进行快速排序以及其背后的实现原理。首先,让我们了解快速排序的基本概念。快速排序是一种高效的排序算法,由英国计算机科学家C.A.R.
js对象数组按属性快速排序
"该资源主要讨论了如何使用JavaScript对对象数组进行快速排序,特别是根据对象的特定属性进行排序。提供了一种洗牌算法(getRandomPlayCard)和一个基于属性的快速排序函数(sort
js实现数组冒泡排序、快速排序原理
最后,通过递归调用快速排序函数并合并结果,实现整个数组的排序。快速排序的平均时间复杂度为O(n log n),最坏情况(即输入数组已完全排序或逆序)为O(n^2)。


