You still want to sort the array, so you need to call Array.Sort with
mp3 as one parameter - but there's an overload which takes an IComparer
(or one which takes an IComparer<T> if you're using .NET 2.0) which is
used for the sorting. You need to implement IComparer in some class or
other to descibe the ordering you want.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
And to extend on what Jon was stating...the following is v1.1 code and uses
the IComparable interface instead of the IComparer interface. The MP3File
class implements this interface and containts the implementation of
CompareTo, which is all Array.Sort method needs to sort the class (notice,
it is no longer just a struct, but a full class w/o comments).
public class MP3File : IComparable
{
#region Private Members
// =============== =============== =============== =============== =====
// Private Members
// =============== =============== =============== =============== =====
private int mTrackNumber;
private int mLength;
private string mFileName;
private string mArtist;
private string mTitle;
#endregion
#region Constructors / Destructors
// =============== =============== =============== =============== =====
// Constructors / Destructors
// =============== =============== =============== =============== =====
public MP3File() { }
#endregion
#region Public Properties
// =============== =============== =============== =============== =====
// Public Properties
// =============== =============== =============== =============== =====
public int TrackNumber
{
get { return mTrackNumber; }
set { mTrackNumber = value; }
}
public int Length
{
get { return mLength; }
set { mLength = value; }
}
public string FileName
{
get { return mFileName; }
set { mFileName = value; }
}
public string Artist
{
get { return mArtist; }
set { mArtist = value; }
}
public string Title
{
get { return mTitle; }
set { mTitle = value; }
}
#endregion
#region Public Methods
// =============== =============== =============== =============== =====
// Public Methods
// =============== =============== =============== =============== =====
public int CompareTo(objec t obj)
{
if (!(obj is MP3File)) {
throw new ArgumentExcepti on(
"Argument 'obj' is not of type MP3File.",
"obj"
);
}
int num = ((MP3File) obj).TrackNumbe r;
if (this.TrackNumb er < num) {
return -1;
} else if (this.TrackNumb er == num) {
return 0;
} else {
return 1;
}
}
public override string ToString()
{
return string.Format(
"{0} : {1} : {2}",
this.TrackNumbe r,
this.Artist,
this.Title
);
}
#endregion
}
/// The following class should be created and the Mp3Files method called to
/// test the functionality of the MP3File and IComparable.
public class Class1
{
public Class1() { }
public void Mp3Files()
{
MP3File[] files = new MP3File[3];
files[0] = new MP3File();
files[0].TrackNumber = 3;
files[0].Length = 5000;
files[0].Artist = "Me";
files[0].FileName = "MyFileName 3";
files[0].Title = "Title 3";
files[1] = new MP3File();
files[1].TrackNumber = 1;
files[1].Length = 23422;
files[1].Artist = "My";
files[1].FileName = "MyFileName 1";
files[1].Title = "Title 1";
files[2] = new MP3File();
files[2].TrackNumber = 2;
files[2].Length = 23421;
files[2].Artist = "Mine";
files[2].FileName = "MyFileName 2";
files[2].Title = "Title 2";
Array.Sort(file s);
foreach (MP3File file in files) {
Console.WriteLi ne(file);
}
}
}
:)
HTH some more.
Mythran