using System; using System.Collections; public class SamplesArrayList { 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( "jumps" ); 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. myAL.Sort(); // Displays the values of the ArrayList. Console.WriteLine( "After sorting:" ); 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(); } } /* This code produces the following output.The ArrayList initially contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dogAfter sorting: [0]: brown [1]: dog [2]: fox [3]: jumps [4]: lazy [5]: over [6]: quick [7]: the [8]: The */
ArrayList arrayList = new ArrayList(); . . . arrayList.Sort();
new 个arraylist就看到了.
using System.Collections;
public class SamplesArrayList { 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( "jumps" );
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.
myAL.Sort(); // Displays the values of the ArrayList.
Console.WriteLine( "After sorting:" );
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();
}
}
/*
This code produces the following output.The ArrayList initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dogAfter sorting:
[0]: brown
[1]: dog
[2]: fox
[3]: jumps
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The
*/
.
.
.
arrayList.Sort();
http://blog.csdn.net/chengking/archive/2005/10/07/496720.aspx
如果要效率,自己写一个排序的方法,具体代码见《数据结构》。
如果要方便,楼上的Blog里写的很清楚。
using System;namespace Zhengzuo
{
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();
a.Add("11") ;
a.Add("12") ;
a.Add("3") ;
a.Add("8") ;
a.Sort(); //默认为从小到大排序
a.Reverse() ; //将原来的顺序进行反转
ArrayList a = new ArrayList() ;
a.Add(11) ;
a.Add(12) ;
a.Add(3) ;
a.Add(8) ;
a.Sort(); //默认为从小到大排序
a.Reverse() ; //将原来的顺序进行反转
using System;namespace Zhengzuo
{
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();