简体   繁体   English

我将如何对结构数组进行排序?

[英]How would I sort through an array of structs?

I have a struct containing song data:我有一个包含歌曲数据的结构:

public struct uLib
    {
        public string Path;
        public string Artist;
        public string Title;
        public string Album;
        public string Length;
    }  

My library consists of an array of this uLib .我的库包含这个uLib的数组。 How would I sort this array by say Artist?我将如何通过说艺术家对这个数组进行排序? Is there a native sort function I can call on this type of array, or will I have to "roll my own"?是否有本地排序 function 我可以调用这种类型的数组,还是必须“自己动手”?

First of all, that should not be a struct.首先,那不应该是一个结构。 It's larger than 16 bytes, so you don't get the performance benefits of having a struct.它大于 16 个字节,因此您无法获得结构体的性能优势。 Also, it doesn't represent a single value, so it doesn't make sense semantically to make it a struct.此外,它不代表单个值,因此使其成为结构在语义上没有意义。 Just make it a class instead.只需将其改为 class 即可。

The Array class has a Sort method that you can use: Array class 有一个可以使用的Sort方法:

Array.Sort(theArray, (x,y) => string.Compare(x.Artist,y.Artist));

If you don't have C# 3 you use a delegate instead of the lambda expression:如果您没有 C# 3 ,则使用委托而不是 lambda 表达式:

Array.Sort(theArray, delegate(uLib x, uLib y) { return string.Compare(x.Artist,y.Artist) } );

Edit:编辑:
Here's an example of what your data could look like as a class:以下是您的数据可能看起来像 class 的示例:

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;
    }

}

In C# there there is a short form for a property.在 C# 中有一个属性的简短形式。 Instead of writing code for a private variable and a setter and getter to access it, this creates that automatically:这不是为私有变量和 setter 和 getter 编写代码来访问它,而是自动创建它:

public string Path { get; set; }

from u in ULibArray order by u.Artist select u; u.Artist select u 以 ULibArray 顺序从 u 开始;

Assuming uLibs is an IEnumerable<T> , you can try this:假设uLibs是一个IEnumerable<T> ,你可以试试这个:

uLibs.OrderBy(i => i.Artist)

This sorts the uLib instances by using a key;这通过使用键对uLib实例进行排序; in this case, you've selected Artist to be the key to compare against.在这种情况下,您选择了艺术家作为比较的关键。 Similar sorting is possible against your other fields.可以对您的其他字段进行类似的排序。

Doesn't Sort array of items using OrderBy<> answer your question? 使用 OrderBy<> 对项目数组进行排序不能回答您的问题吗?

Regards Friedrich问候弗里德里希

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM