佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 894|回复: 2

如何实现 sortOn ?

[复制链接]
发表于 30-5-2006 10:34 PM | 显示全部楼层 |阅读模式

sortOn ,有用 Java 的朋友应该很熟悉·Array.sortOn()。
也就是多项排序。
不知 C# 里应该有类似的功能,但小章鱼没有找到。
总该不会叫咱们用泡排序(bubble sort)来自己实现吧。

小章鱼不会解释,直接用例子:
  1. class myObj {
  2.     private int myNum1;
  3.     private int myNum2;
  4.     public void myObj(int a, int b) {
  5.         myNum1 = a;
  6.         myNum2 = b;
  7.     }
  8.     public int Num1 {
  9.         get { return myNum1; }
  10.     }
  11.     public int Num2 {
  12.         get { return myNum2; }
  13.     }
  14. }

  15. Array.Add(new myObj(0,1));
  16. Array.Add(new myObj(2,3));
  17. Array.Add(new myObj(1,2));
  18. Array.Add(new myObj(3,0));
  19. Array.Add(new myObj(0,3));

  20. //未使用 sortOn 前,结果
  21. Num1 : 0                Num2 : 1
  22. Num1 : 2                Num2 : 3
  23. Num1 : 1                Num2 : 2
  24. Num1 : 3                Num2 : 0
  25. Num1 : 0                Num2 : 3

  26. Array.sortOn("Num1","Num2"); //结果
  27. Num1 : 0                Num2 : 1
  28. Num1 : 0                Num2 : 3
  29. Num1 : 1                Num2 : 2
  30. Num1 : 2                Num2 : 3
  31. Num1 : 3                Num2 : 0

  32. Array.sortOn("Num2","Num1");//结果
  33. Num1 : 3                Num2 : 0
  34. Num1 : 0                Num2 : 1
  35. Num1 : 1                Num2 : 2
  36. Num1 : 0                Num2 : 3
  37. Num1 : 2                Num2 : 3

  38. 可以是任意项 sortOn(a,b,c,d...)
复制代码

不知有何效率好的方法?
最好又是简单的

小章鱼猜想应该是用 IComparable 这个咚咚吧,
不过就是对它完全没有思路。
回复

使用道具 举报


ADVERTISEMENT

发表于 1-6-2006 09:09 AM | 显示全部楼层
其实不难, 只需要定义IComparer.Compare, 这是MSDN Library的范例:


  1. using System;
  2. using System.Collections;

  3. public class SamplesArray  {

  4.    public class myReverserClass : IComparer  {
  5.       // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
  6.       int IComparer.Compare( Object x, Object y )  {
  7.           return( (new CaseInsensitiveComparer()).Compare( y, x ) );
  8.       }

  9.    }

  10.    public static void Main()  {

  11.       // Creates and initializes a new Array and a new custom comparer.
  12.       String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
  13.       IComparer myComparer = new myReverserClass();

  14.       // Displays the values of the Array.
  15.       Console.WriteLine( "The Array initially contains the following values:" );
  16.       PrintIndexAndValues( myArr );

  17.       // Sorts a section of the Array using the default comparer.
  18.       Array.Sort( myArr, 1, 3 );
  19.       Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
  20.       PrintIndexAndValues( myArr );

  21.       // Sorts a section of the Array using the reverse case-insensitive comparer.
  22.       Array.Sort( myArr, 1, 3, myComparer );
  23.       Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
  24.       PrintIndexAndValues( myArr );

  25.       // Sorts the entire Array using the default comparer.
  26.       Array.Sort( myArr );
  27.       Console.WriteLine( "After sorting the entire Array using the default comparer:" );
  28.       PrintIndexAndValues( myArr );

  29.       // Sorts the entire Array using the reverse case-insensitive comparer.
  30.       Array.Sort( myArr, myComparer );
  31.       Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
  32.       PrintIndexAndValues( myArr );

  33.    }

  34.    public static void PrintIndexAndValues( String[] myArr )  {
  35.       for ( int i = 0; i < myArr.Length; i++ )  {
  36.          Console.WriteLine( "   [{0}] : {1}", i, myArr[i] );
  37.       }
  38.       Console.WriteLine();
  39.    }
  40. }

复制代码
回复

使用道具 举报

 楼主| 发表于 4-6-2006 10:28 AM | 显示全部楼层
Jezz 虽然感觉你有点不了解小章鱼的问题,不过还是要谢谢你。
小章鱼是要寻找一个简单有效的方法来实现多项 properties 排序。
你所抄出来的例子只是普通的排序吧。

没关系,小章鱼已经用最原始的方法来达到——就是做多次比较

如果那位有好方法麻烦提供。谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 5-8-2025 03:54 AM , Processed in 0.123988 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表