问题描述
您好,
是否可以对包含结构的数组进行排序?
示例:
我有一个结构:
public struct mp3file
{
public int tracknr;
public int length;
公共字符串文件名;
公共字符串艺术家;
公共字符串标题;
}
而且我有这个:
mp3file [] mp3 = new mp3file [files.Length];
int t = 0;
foreach(FileInfo fi in files)
{
MP3File mp3File = ShellID3TagReader.ReadID3Tags(fi .FullName);
mp3 [t] .length = mp3File.Length;
mp3 [t] .artist = mp3File.ArtistName;
mp3 [t] .title = mp3File.SongTitle;
mp3 [t] .tracknr = mp3File.TrackNumber;
mp3 [t] .filename = mp3File.FileName;
t ++;
}
这是有效的,但现在我想通过tracknr对数组进行排序,所以我可以做
a播放列表,所以由tracknr执行。
类似于:
Array.Sort(mp3.tracknr)
但是不工作
我该怎么做?
感谢您的帮助!
-
Hello,
Is it possible to sort an array with a struct in it?
example:
I have a struct:
public struct mp3file
{
public int tracknr;
public int length;
public string filename;
public string artist;
public string title;
}
and than I have this:
mp3file[] mp3 = new mp3file[files.Length];
int t = 0;
foreach (FileInfo fi in files)
{
MP3File mp3File = ShellID3TagReader.ReadID3Tags(fi.FullName);
mp3[t].length = mp3File.Length;
mp3[t].artist = mp3File.ArtistName;
mp3[t].title = mp3File.SongTitle;
mp3[t].tracknr = mp3File.TrackNumber;
mp3[t].filename = mp3File.FileName;
t++;
}
this is working, but now I want to sort the array by tracknr, so I can make
a playlist, sorted by tracknr.
with something like:
Array.Sort(mp3.tracknr)
but that doesn''t work
How can I do this?
Thanks for your help!
--
www.gsnel.nl
推荐答案
您仍然希望对数组进行排序,因此您需要使用
mp3作为一个参数调用Array.Sort - 但是那里'需要一个IComparer
的过载(如果你使用的是.NET 2.0,则需要IComparer< T>),这是用于排序的
。你需要在某个类中实现IComparer或者
其他来描述你想要的订单。
-
Jon Skeet - < sk *** @ pobox.com>
博客:
如果回复小组,请不要给我发邮件
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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
你有创建IComparer类。以下*应该*是你需要的b $ b $
公共结构mp3文件
{
public int tracknr;
public int length;
公共字符串文件名;
公共字符串艺术家;
public字符串标题;
}
公共类Class1
{
public Class1 (){}
public void Mp3Files()
{
mp3file [] files = new mp3file [2];
files [0] .tracknr = 2;
files [0] .length = 5000;
files [0] .filename =" blah2" ;;
files [0] .title =" Title 2";
files [1] .tracknr = 1;
files [1] .length = 5000;
files [1] .filename =" blah1";
files [1] .title =" Title 1";
Array.Sort(文件,新的Mp3FileComparer());
foreach(文件中的mp3file文件){
Console.WriteLine (file.tracknr);
}
}
}
公共类Mp3FileComparer:System.Collections.IComparer
{
public Mp3FileComparer(){}
public int Compare(object Object1,object Object2)
{
if(!(Object1 is mp3file)){
抛出新的ArgumentException(
" Object1必须是mp3file类型。",
" Object1"
);
}否则if(!(Object2是mp3file)){
抛出新的ArgumentException(
" Object2必须是mp3file类型。),
" Object2"
);
}
int n1 =((mp3file)Object1).tracknr;
int n2 =((mp3file)Object2).tracknr;
if(n1> n2){
返回1;
}否则if(n1 == n2){
返回0;
}否则{
返回-1;
}
}
}
HTH :)
Mythran
You''d have to create an IComparer class. The following *should* be what you
need :)
public struct mp3file
{
public int tracknr;
public int length;
public string filename;
public string artist;
public string title;
}
public class Class1
{
public Class1() { }
public void Mp3Files()
{
mp3file[] files = new mp3file[2];
files[0].tracknr = 2;
files[0].length = 5000;
files[0].filename = "blah2";
files[0].title = "Title 2";
files[1].tracknr = 1;
files[1].length = 5000;
files[1].filename = "blah1";
files[1].title = "Title 1";
Array.Sort(files, new Mp3FileComparer());
foreach (mp3file file in files) {
Console.WriteLine(file.tracknr);
}
}
}
public class Mp3FileComparer : System.Collections.IComparer
{
public Mp3FileComparer() { }
public int Compare(object Object1, object Object2)
{
if (!(Object1 is mp3file)) {
throw new ArgumentException(
"Object1 must be of type mp3file.",
"Object1"
);
} else if (!(Object2 is mp3file)) {
throw new ArgumentException(
"Object2 must be of type mp3file.",
"Object2"
);
}
int n1 = ((mp3file) Object1).tracknr;
int n2 = ((mp3file) Object2).tracknr;
if (n1 > n2) {
return 1;
} else if (n1 == n2) {
return 0;
} else {
return -1;
}
}
}
HTH :)
Mythran
这篇关于Array.Sort结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!