Kra*_*jak 3 .net c# arrays sorting
我有一个包含歌曲数据的结构:
public struct uLib
{
public string Path;
public string Artist;
public string Title;
public string Album;
public string Length;
}
Run Code Online (Sandbox Code Playgroud)
我的库包含了一个这样的数组uLib.我怎么用艺术家对这个阵列进行排序?我可以在这种类型的数组上调用本机排序函数,还是必须"自己动手"?
Guf*_*ffa 16
首先,这不应该是一个结构.它大于16个字节,因此您无法获得具有结构的性能优势.此外,它不代表单个值,因此在语义上使其成为结构是没有意义的.只是让它成为一个类.
本Array类有一个Sort可以使用的方法:
Array.Sort(theArray, (x,y) => string.Compare(x.Artist,y.Artist));
Run Code Online (Sandbox Code Playgroud)
如果您没有C#3,则使用委托而不是lambda表达式:
Array.Sort(theArray, delegate(uLib x, uLib y) { return string.Compare(x.Artist,y.Artist) } );
Run Code Online (Sandbox Code Playgroud)
编辑:
以下是您的数据作为类的外观示例:
public class ULib {
private string _path, _artist, _title, _album, _length;
public string Path { get { return _path; } set { _path = value; } }
public string Artist { get { return _artist; } set { _artist = value; } }
public string Title { get { return _title; } set { _title = value; } }
public string Album { get { return _album; } set { _album = value; } }
public string Length { get { return _length; } set { _length = value; } }
public ULib() {}
public ULib(string path, string artist, string title, string album, string length) {
Path = path;
Artist = artist;
Title = title;
Album = album;
Length = length;
}
}
Run Code Online (Sandbox Code Playgroud)
在C#中,有一个简短的财产形式.不是为私有变量编写代码,而是使用setter和getter来访问它,而是自动创建:
public string Path { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8266 次 |
| 最近记录: |