看到推荐贴, 我也来发一个实体类排序的, 用快速排序法, 可以依据多个字段排序

依然白板 2010-05-17 05:26:21
        /// <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;
}
}
...全文
200 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
醒子宇 2010-05-24
  • 打赏
  • 举报
回复
PL!
mohugomohu 2010-05-20
  • 打赏
  • 举报
回复
good!
依然白板 2010-05-17
  • 打赏
  • 举报
回复
        /// <summary>
/// 给实体数组排序, 可以用依据多个属性的大小进行排序。
/// </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;
}
}


改了一下, 还要加什么
马老虎 2010-05-17
  • 打赏
  • 举报
回复
仔细看看!
依然白板 2010-05-17
  • 打赏
  • 举报
回复
非要用中文注释么,
那晚点来改, 装了VS的那台机器没有中文输入法
兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
by Chinese please
兔子-顾问 2010-05-17
  • 打赏
  • 举报
回复
不是这么弄的,你要先说你这个代码是干嘛的,算法的东西本来就不直观,还没功能说明会很郁闷的。
mohugomohu 2010-05-17
  • 打赏
  • 举报
回复
看不懂啊
gxingmin 2010-05-17
  • 打赏
  • 举报
回复
顶一个
Justin-Liu 2010-05-17
  • 打赏
  • 举报
回复
SF 哈哈
企业智慧中台规划与建设总体方案是一个针对企业数字化转型需求而设计的综合性IT解决方案。该方案旨在构建一个集中、高效和灵活的中台架构,通过整合企业内外部的数据资源、业务流程和应用服务,为企业提供一个统一的数字化运营平台。在当前的商业环境下,企业面临着激烈的市场竞争和不断变化的客户需求。为了应对这些挑战,企业需要快速响应市场变化,提高运营效率,降低成本,并实现持续创新。因此,企业智慧中台的规划和建设成为了关键任务。该方案的核心思想是将企业的业务逻辑、数据管理和技术创新紧密结合起来,形成一个高度集成的智慧中台系统。通过构建标准化的数据模型和接口,实现数据的一致性和可访问性,同时提供丰富的数据分析和挖掘工具,帮助企业现潜在的商业价值和机会。此外,该方案还注重用户体验和业务敏捷性。通过构建灵活的应用开框架和API接口,支持快速的业务迭代和定制化需求,满足不同部门和角色的个性化需求。同时,通过智能化的自动化流程和决策支持系统,提高企业的决策效率和准确性。总之,企业智慧中台规划与建设总体方案是一个全面而深入的IT解决方案,旨在帮助企业实现数字化转型,提升竞争力和创新能力。通过构建集中、高效和灵活

110,534

社区成员

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

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

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