How can I sort an array of structures?

I have a structure 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 can I sort this array, say, Artist? Is there any sort function of my own that I can call in this type of array, or do I have to "collapse my own"?

+3
source share
4 answers

First of all, this should not be a structure. It is more than 16 bytes, so you do not get performance benefits from the structure. In addition, it does not represent a single meaning, so it does not make sense semantically to make it a structure. Just make a class instead.

Array Sort, :

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

# 3, :

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

Edit:
, :

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

}

# . , getter , :

public string Path { get; set; }
+16

u ULibArray u.Artist select u;

+1

, uLibs IEnumerable<T>, :

uLibs.OrderBy(i => i.Artist)

uLib ; Artist . .

0

Not Sorting an array of elements using OrderBy <gt; answer your question?

Relations Friedrich

0
source

Source: https://habr.com/ru/post/1704570/


All Articles