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的高手们......
期待中。

解决方案 »

  1.   

    我的建议是这样:用typeof得到类型
    首先建立一个类型的先后顺序,比如:首先排序值类型,然后排序引用类型
    值类型首先排序int型,然后是short和ubyte
    其次排序浮点型
    引用类型中发现是字符串的,进行字符串排序
    然后把typeof的类名tostring,按照类名的字符串进行排序
    …………
    唉!你的需求好变态哦
      

  2.   

    下面的示例显示如何使用默认比较器和自定义的反序比较器,对 ArrayList 中的某个部分的值进行排序。[Visual Basic] 
    Imports System
    Imports System.Collections
    Imports Microsoft.VisualBasicPublic 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();   }}
    按照你的需求实现自定义比较器吧
      

  3.   

    先看一个例子,
    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)
    {}
      

  4.   

    to zhzuo(秋枫)  我要要是连这个都不知道我就不是程序员了,问题就是那个 CompareTo 函数
    to web_gus(penny 路漫漫其修远兮,吾将上下而求索): 不知从哪里贴来的文章,请看青问题我的问题就是,对于一些不同数据类型并且已经装了箱的值类型数据怎样比较大小,这些数据类型有以下组成:byte sbyte short ushort int uint long ulong float double decimal.
      

  5.   

    to ah__fu(阿福): 这种方式能够解决,但是这样的代码写出来会很长的,如果把程序作为一种艺术,这种代码是不应该出现在软件中的.
      

  6.   

    to Microsoft MVP:  你们能够解决这个问题么?
      

  7.   

    如果你能保证每个元素是可比较的用
    ArrayList.Sort(
       null
    )
    或者自己提供 IComparer 接口
      

  8.   

    to uscool(小小风):
    想过性能了么?
      

  9.   

    不同类型,没有偷懒办法,自己实现IComparable借口。