查看: 894|回复: 2
|
如何实现 sortOn ?
[复制链接]
|
|
sortOn ,有用 Java 的朋友应该很熟悉·Array.sortOn()。
也就是多项排序。
不知 C# 里应该有类似的功能,但小章鱼没有找到。
总该不会叫咱们用泡排序(bubble sort)来自己实现吧。
小章鱼不会解释,直接用例子:- class myObj {
- private int myNum1;
- private int myNum2;
- public void myObj(int a, int b) {
- myNum1 = a;
- myNum2 = b;
- }
- public int Num1 {
- get { return myNum1; }
- }
- public int Num2 {
- get { return myNum2; }
- }
- }
- Array.Add(new myObj(0,1));
- Array.Add(new myObj(2,3));
- Array.Add(new myObj(1,2));
- Array.Add(new myObj(3,0));
- Array.Add(new myObj(0,3));
- //未使用 sortOn 前,结果
- Num1 : 0 Num2 : 1
- Num1 : 2 Num2 : 3
- Num1 : 1 Num2 : 2
- Num1 : 3 Num2 : 0
- Num1 : 0 Num2 : 3
- Array.sortOn("Num1","Num2"); //结果
- Num1 : 0 Num2 : 1
- Num1 : 0 Num2 : 3
- Num1 : 1 Num2 : 2
- Num1 : 2 Num2 : 3
- Num1 : 3 Num2 : 0
- Array.sortOn("Num2","Num1");//结果
- Num1 : 3 Num2 : 0
- Num1 : 0 Num2 : 1
- Num1 : 1 Num2 : 2
- Num1 : 0 Num2 : 3
- Num1 : 2 Num2 : 3
- 可以是任意项 sortOn(a,b,c,d...)
复制代码
不知有何效率好的方法?
最好又是简单的
小章鱼猜想应该是用 IComparable 这个咚咚吧,
不过就是对它完全没有思路。 |
|
|
|
|
|
|
|
发表于 1-6-2006 09:09 AM
|
显示全部楼层
其实不难, 只需要定义IComparer.Compare, 这是MSDN Library的范例:
- using System;
- using System.Collections;
- public class SamplesArray {
- public class myReverserClass : IComparer {
- // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
- int IComparer.Compare( Object x, Object y ) {
- return( (new CaseInsensitiveComparer()).Compare( y, x ) );
- }
- }
- public static void Main() {
-
- // Creates and initializes a new Array and a new custom comparer.
- String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
- IComparer myComparer = new myReverserClass();
-
- // Displays the values of the Array.
- Console.WriteLine( "The Array initially contains the following values:" );
- PrintIndexAndValues( myArr );
-
- // Sorts a section of the Array using the default comparer.
- Array.Sort( myArr, 1, 3 );
- Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
- PrintIndexAndValues( myArr );
- // Sorts a section of the Array using the reverse case-insensitive comparer.
- Array.Sort( myArr, 1, 3, myComparer );
- Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
- PrintIndexAndValues( myArr );
- // Sorts the entire Array using the default comparer.
- Array.Sort( myArr );
- Console.WriteLine( "After sorting the entire Array using the default comparer:" );
- PrintIndexAndValues( myArr );
- // Sorts the entire Array using the reverse case-insensitive comparer.
- Array.Sort( myArr, myComparer );
- Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
- PrintIndexAndValues( myArr );
- }
-
- public static void PrintIndexAndValues( String[] myArr ) {
- for ( int i = 0; i < myArr.Length; i++ ) {
- Console.WriteLine( " [{0}] : {1}", i, myArr[i] );
- }
- Console.WriteLine();
- }
- }
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 4-6-2006 10:28 AM
|
显示全部楼层
Jezz 虽然感觉你有点不了解小章鱼的问题,不过还是要谢谢你。
小章鱼是要寻找一个简单有效的方法来实现多项 properties 排序。
你所抄出来的例子只是普通的排序吧。
没关系,小章鱼已经用最原始的方法来达到——就是做多次比较
如果那位有好方法麻烦提供。谢谢 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|