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. 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’?
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. 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.
The
Arrayclass has aSortmethod that you can use:If you don’t have C# 3 you use a delegate instead of the lambda expression:
Edit:
Here’s an example of what your data could look like as a class:
In C# there there is a short form for a property. Instead of writing code for a private variable and a setter and getter to access it, this creates that automatically: