简体   繁体   English

无法按C#中的Tag.AlbumArtists排序

[英]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. 我正在尝试按艺术家然后按专辑对一组mp3文件进行排序。 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. 列表“ sortedByBand”及其附带的sort导致以下消息:附加信息:至少一个对象必须实现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. 看起来AlbumArtists是某种枚举(如字符串数组),无法与其他实例进行比较,因此listOfFiles不能由此排序。

In this case, you'll have to convert AlbumArtists to something that can be compared with other instances. 在这种情况下,您必须将AlbumArtists转换为可以与其他实例进行比较的对象。 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"} . 当然,这假设具有艺术家{"Pink Floyd", "U2"}的MP3将 {"U2", "Pink Floyd"}的MP3 之前出现。 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 我认为您的方法可能无法得出正确的结果,您不应该进行双重排序,我的LINQ提供了扩展然后进行排序,因为您使用字符串数组作为排序元素,但您会收到该错误,我不确定是否可能,但根据其他建议,您可以尝试

///  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();

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

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