Array.Sort结构 [英] Array.Sort a struct

查看:54
本文介绍了Array.Sort结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


是否可以对包含结构的数组进行排序?


示例:


我有一个结构:

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)


但是不工作


我该怎么做?


感谢您的帮助!

-

www.gsnel.nl

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

推荐答案

Gerrit< gs ************* @ hotmail.com>写道:
Gerrit <gs*************@hotmail.com> wrote:
是否可以对包含结构的数组进行排序?

例如:

我有一个结构:
public struct mp3file
{public int tracknr;
public int length;
公共字符串文件名;
公共字符串艺术家;
公共字符串标题;
}


为什么这是一个结构?这对我来说似乎是一个坏主意。 (顺便说一句,我也会给你b $ b建议你没有公共场地。)

而不是我有这个:


< snip>

这是有效的,但现在我想通过tracknr对数组进行排序,这样我就可以制作一个按tracknr排序的播放列表。 />有类似的东西:

Array.Sort(mp3.tracknr)

但是那不起作用

我怎么能这样做?
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;
}
Why is this a struct? That seems like a bad idea to me. (I''d also
suggest that you don''t have public fields, by the way.)
and than I have this:
<snip>
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?




您仍然希望对数组进行排序,因此您需要使用

mp3作为一个参数调用Array.Sort - 但是那里'需要一个IComparer

的过载(如果你使用的是.NET 2.0,则需要IComparer< T>),这是用于排序的
。你需要在某个类中实现IComparer或者

其他来描述你想要的订单。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet 博客: http://www.msmvps。 com / jon.skeet

如果回复小组,请不要给我发邮件



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




" Gerrit" < GS ************* @ hotmail.com>在消息中写道

news:S0REf.4732

"Gerrit" <gs*************@hotmail.com> wrote in message
news:S0REf.4732


zc1.1422@amstwist00 ...
zc1.1422@amstwist00...
你好,

是否可以对带有结构的数组进行排序?

示例:

我有一个结构:
public struct mp3file
{
public int tracknr;
public int length;
公共字符串文件名;
公共字符串艺术家;
公共字符串标题;
}

而且我有这个:

mp3file [] mp3 = new mp3file [files.Length];
int t = 0;

foreach( FileInfo fi in files)
{MP3文件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对数组进行排序,所以我可以制作一个播放列表,按tracknr排序。
使用类似的东西:

Array.Sort(mp3.tracknr)

但是没有工作

我该怎么做?

感谢您的帮助!
-

www.gsnel.nl




你有创建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结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆