简体   繁体   中英

Not able to sort by Tag.AlbumArtists in c#

I'm trying to sort a group of mp3 files by artist and then by album. In the course of doing this I have come across two statements; one that works and one that doesn't. I was under the impression that to do both of the sorts I want to do all I had to do was to use them one after the other, but I can't do that because one of them doesn't work. See my code below:

foreach (string file in files)
{
    TagLib.File fi = TagLib.File.Create(file);
    listOfFiles.Add(fi);
}
List<TagLib.File> sortedByBand = listOfFiles.OrderBy(o =>
o.Tag.AlbumArtists).ToList();
List<TagLib.File> sortedBy = listOfFiles.OrderBy(o => 
o.Tag.AlbumArtists).ToList();

The list "sortedByBand" and it's accompanying sort results in the following message: Additional information: At least one object must implement IComparable. Thanks in advance for any and all help rendered.

It looks like AlbumArtists is some sort of enumeration (like an array of strings), which can't be compared to other instances thus listOfFiles can't be sorted by it.

In this case, you'll have to convert AlbumArtists to something that can be compared with other instances. For example, a string:

List<TagLib.File> sortedByBand = listOfFiles.OrderBy(o =>
String.Join(";", o.Tag.AlbumArtists)).ToList();

This, of course, assumes that an MP3 with the artists {"Pink Floyd", "U2"} will occur before an MP3 of {"U2", "Pink Floyd"} . To avoid this, you'd want to sort the artist list itself before converting to a single string. Hope this helps!

I think your approach might not lead correct results, you should not do double sort, I thin LINQ offers an extension to sort then by, since you are using the array of strings as your sort element you getting that error I am not sure if it possible but based on the other suggestion you can try

///  sort the array inside the tag.

    listOfFiles.ForEach(x =>Array.Sort(o.Tag.AlbumArtists));
///if this does not work try other suggestion 
    List<TagLib.File> sortedByBand=listOfFiles.OrderBy(o =>o.Tag.AlbumArtists.FirstOrDefault()).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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