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的高手们......
期待中。
// 这不是我创建的,但是我一定要想办法进行排序或者比较
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的高手们......
期待中。
解决方案 »
- SOAOfficeCtrl绿色版,能用js进行单元格内容的控制访问吗?若能解决,我将另开高分
- C# 判断是否插入U盾
- 最小内存区间取内容
- 如何在js中判断,选中的.jpg图片是Web所支持的.jpg图片????
- 如何实现 动态 委托
- 急!!!C#初学者,多个textbox如何写判断语句
- 请教:System.Windows.Forms 问题
- 如何在生成的水晶报表中添加打印预览功能?
- 如何用一个图像a产生别一个图像b,这个b是A大小的一半,用什么方法进行缩放(不是用DrawImage).
- 今天听了微软的讲座(深圳),还装了“送的”vs.net bete2 中文版。
- 如何得到WebBrowser控件里网页的源代码??
- 请问大家一个关于winform的问题?
首先建立一个类型的先后顺序,比如:首先排序值类型,然后排序引用类型
值类型首先排序int型,然后是short和ubyte
其次排序浮点型
引用类型中发现是字符串的,进行字符串排序
然后把typeof的类名tostring,按照类名的字符串进行排序
…………
唉!你的需求好变态哦
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(); }}
按照你的需求实现自定义比较器吧
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)
{}
to web_gus(penny 路漫漫其修远兮,吾将上下而求索): 不知从哪里贴来的文章,请看青问题我的问题就是,对于一些不同数据类型并且已经装了箱的值类型数据怎样比较大小,这些数据类型有以下组成:byte sbyte short ushort int uint long ulong float double decimal.
ArrayList.Sort(
null
)
或者自己提供 IComparer 接口
想过性能了么?