再问一个关于不同类型比较的问题(有难度哦!)

Nepton 2005-05-28 07:08:04
ArrayList al = new ArrayList();

// 创建的这个数组里边就是不同的数据类型,
// 这不是我创建的,但是我一定要想办法进行排序或者比较
al.Add(1);
al.Add(2L);
al.Add(3f);
al.Add(4d);
al.Add(5m);

al.Sort(); // 执行到这一句一定出错,肯定是一格类型转换错误,可能只有自己写这个比较器,很不容易写哦!有10多种数据类型 ^_^
foreach(object obj in al)
{
Console.WriteLine(obj.ToString());
}

前几天问了一个类似的问题,无人能解决
http://community.csdn.net/Expert/topic/4035/4035898.xml?temp=.5465052
莫非CSDN的高手们......
期待中。
...全文
177 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
marvelstack 2005-07-18
  • 打赏
  • 举报
回复
不同类型,没有偷懒办法,自己实现IComparable借口。
Nepton 2005-07-17
  • 打赏
  • 举报
回复
to uscool(小小风):
想过性能了么?
uscool 2005-06-07
  • 打赏
  • 举报
回复
很简单的问题,自己写个排序函数不就OK了?!你将都是以字符对待
stonegoldaustin 2005-06-07
  • 打赏
  • 举报
回复
重载运算符,楼主自己考虑考虑。
真相重于对错 2005-06-07
  • 打赏
  • 举报
回复
如果你能保证每个元素是可比较的用
ArrayList.Sort(
null
)
或者自己提供 IComparer 接口
真相重于对错 2005-06-07
  • 打赏
  • 举报
回复
没有简单的方法,只有自己写。
bmiracle 2005-06-07
  • 打赏
  • 举报
回复
顶,看有没有高手来解决
zr1982930 2005-06-07
  • 打赏
  • 举报
回复
继续顶!
CCsdnCC 2005-06-07
  • 打赏
  • 举报
回复
看了大家的回复,也做了实验。感觉只有一个:有人喜欢卖弄。(如果是学生也就原谅了,但故弄悬虚也让人反感)
Nepton 2005-06-06
  • 打赏
  • 举报
回复
to ah__fu(阿福): 这种方式能够解决,但是这样的代码写出来会很长的,如果把程序作为一种艺术,这种代码是不应该出现在软件中的.
Nepton 2005-06-06
  • 打赏
  • 举报
回复
to zhzuo(秋枫) 我要要是连这个都不知道我就不是程序员了,问题就是那个 CompareTo 函数
to web_gus(penny 路漫漫其修远兮,吾将上下而求索): 不知从哪里贴来的文章,请看青问题

我的问题就是,对于一些不同数据类型并且已经装了箱的值类型数据怎样比较大小,这些数据类型有以下组成:byte sbyte short ushort int uint long ulong float double decimal.
Nepton 2005-06-06
  • 打赏
  • 举报
回复
to Microsoft MVP: 你们能够解决这个问题么?
marvelstack 2005-05-30
  • 打赏
  • 举报
回复
先看一个例子,
using System;

namespace DotManagement.EntryManagement
{
public class FileItem :IComparable
{
private string fileName;

public FileItem(string fileName)
{
this.fileName = fileName;
}
public string FileName
{
get {return this.fileName;}
}


public int CompareTo(object obj)
{

//if (obj == null)
// return 1;

string compareFileName = ((FileItem)obj).FileName;

//if (this.FileName == compareFileName)
// return 0;
//if (this.FileName < compareFileName)
// return -1;
//if (this.FileName > compareFileName)
// return 1;
//return 0;
return this.FileName.CompareTo(compareFileName);
}
}
}
使用
ArrayList al = new ArrayList();
foreach(string fileName in Directory.GetFiles("c;\\"))
{
al.Add(new FileItem(fileName));
}
al.Sort();

//=================
//对于里面的值你喜欢怎么比较就在CompareTo(object obj)怎么写好了。
public int CompareTo(object obj)
{

}
web_gus 2005-05-29
  • 打赏
  • 举报
回复
下面的示例显示如何使用默认比较器和自定义的反序比较器,对 ArrayList 中的某个部分的值进行排序。

[Visual Basic]
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList

Public Class myReverserClass
Implements IComparer

' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Public Function Compare( ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare

End Class 'myReverserClass

Public Shared Sub Main()

' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()
myAL.Add("The")
myAL.Add("QUICK")
myAL.Add("BROWN")
myAL.Add("FOX")
myAL.Add("jumped")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")

' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList initially contains the following values:")
PrintIndexAndValues(myAL)

' Sorts the values of the ArrayList using the default comparer.
myAL.Sort(1, 3, Nothing)
Console.WriteLine("After sorting from index 1 to index 3 with the default comparer:")
PrintIndexAndValues(myAL)

' Sorts the values of the ArrayList using the reverse case-insensitive comparer.
Dim myComparer = New myReverserClass()
myAL.Sort(1, 3, myComparer)
Console.WriteLine("After sorting from index 1 to index 3 with the reverse case-insensitive comparer:")
PrintIndexAndValues(myAL)

End Sub 'Main

Public Shared Sub PrintIndexAndValues(myList As IEnumerable)

Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab + "{1}", i, myEnumerator.Current)
i += 1
End While
Console.WriteLine()

End Sub 'PrintIndexAndValues

End Class 'SamplesArrayList


'This code produces the following output.
'The ArrayList initially contains the following values:
' [0]: The
' [1]: QUICK
' [2]: BROWN
' [3]: FOX
' [4]: jumped
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
'
'After sorting from index 1 to index 3 with the default comparer:
' [0]: The
' [1]: BROWN
' [2]: FOX
' [3]: QUICK
' [4]: jumped
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
'
'After sorting from index 1 to index 3 with the reverse case-insensitive comparer:
' [0]: The
' [1]: QUICK
' [2]: FOX
' [3]: BROWN
' [4]: jumped
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog

[C#]
using System;
using System.Collections;

public class SamplesArrayList {

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 ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "QUICK" );
myAL.Add( "BROWN" );
myAL.Add( "FOX" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the default comparer.
myAL.Sort( 1, 3, null );
Console.WriteLine( "After sorting from index 1 to index 3 with the default comparer:" );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( 1, 3, myComparer );
Console.WriteLine( "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );

}

public static void PrintIndexAndValues( IEnumerable myList ) {

int i = 0;
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
Console.WriteLine();

}

}


按照你的需求实现自定义比较器吧
ah__fu 2005-05-29
  • 打赏
  • 举报
回复
我的建议是这样:用typeof得到类型
首先建立一个类型的先后顺序,比如:首先排序值类型,然后排序引用类型
值类型首先排序int型,然后是short和ubyte
其次排序浮点型
引用类型中发现是字符串的,进行字符串排序
然后把typeof的类名tostring,按照类名的字符串进行排序
…………
唉!你的需求好变态哦
bumm 2005-05-29
  • 打赏
  • 举报
回复
自己重写
Nepton 2005-05-29
  • 打赏
  • 举报
回复
UP, 没有人有办法?
Abel.Liu 2005-05-28
  • 打赏
  • 举报
回复
只能自己写比较了
hjf1223 2005-05-28
  • 打赏
  • 举报
回复
现在没有条件,明天回去试试
mixiaobo 2005-05-28
  • 打赏
  • 举报
回复
看来只有自己动手比较了,为楼主加油
加载更多回复(2)

110,534

社区成员

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

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

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