对包含自定义结构的 ArrayList 进行排序

发布于 2024-11-17 12:38:15 字数 251 浏览 2 评论 0原文

我写了一个结构体

公共结构SeasonEpisodeNr { 公共 int seasonNr; 公共 int EpisodeNr; }

中,我会将这些结构添加到 ArrayList 中。我该如何对它们进行排序?我尝试了 IComparer,但不幸的是我无法理解它是如何工作的。

I wrote a struct

public struct SeasonEpisodeNr
{
public int seasonNr;
public int episodeNr;
}

During my program I will add those structs to an ArrayList. How can I sort them? I tried the IComparer but unfortunately I was not able to understand how it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-11-24 12:38:15

我没有测试过这个,但它就像......

public struct SeasonEpisodeNr: IComparable
{ 
    public int seasonNr; 
    public int episodeNr;
    public int CompareTo(Object Item)
    {
        SeasonEpisodeNr that = (SeasonEpisodeNr) Item;

        if (this.seasonNr > that.seasonNr)
            return -1;
         if (this.seasonNr < that.seasonNr)
            return 1;

         if (this.episodeNr > that.episodeNr)
             return -1;
         if (this.episodeNr < that.episodeNr)
             return 1;

         return 0;
    }

I didn't test this but it's something like...

public struct SeasonEpisodeNr: IComparable
{ 
    public int seasonNr; 
    public int episodeNr;
    public int CompareTo(Object Item)
    {
        SeasonEpisodeNr that = (SeasonEpisodeNr) Item;

        if (this.seasonNr > that.seasonNr)
            return -1;
         if (this.seasonNr < that.seasonNr)
            return 1;

         if (this.episodeNr > that.episodeNr)
             return -1;
         if (this.episodeNr < that.episodeNr)
             return 1;

         return 0;
    }
川水往事 2024-11-24 12:38:15
public struct SeasonEpisodeNr
{
    public SeasonEpisodeNr(int seasonNr, int episodeNr)
    {
        this.seasonNr = seasonNr;
        this.episodeNr = episodeNr;
    }

    public int seasonNr; public int episodeNr; 
}

static void Main(string[] args)
{
    List<SeasonEpisodeNr> list = new List<SeasonEpisodeNr>();
    list.Add(new SeasonEpisodeNr(1, 2));
    list.Add(new SeasonEpisodeNr(1, 1));
    list.Sort((a, b) =>
    {
        //implement comparison, e.g. compare season first and if equal compare the epizods
        int res = a.seasonNr.CompareTo(b.seasonNr);
        return res != 0 ? res : a.episodeNr.CompareTo(b.episodeNr);
    });
}
public struct SeasonEpisodeNr
{
    public SeasonEpisodeNr(int seasonNr, int episodeNr)
    {
        this.seasonNr = seasonNr;
        this.episodeNr = episodeNr;
    }

    public int seasonNr; public int episodeNr; 
}

static void Main(string[] args)
{
    List<SeasonEpisodeNr> list = new List<SeasonEpisodeNr>();
    list.Add(new SeasonEpisodeNr(1, 2));
    list.Add(new SeasonEpisodeNr(1, 1));
    list.Sort((a, b) =>
    {
        //implement comparison, e.g. compare season first and if equal compare the epizods
        int res = a.seasonNr.CompareTo(b.seasonNr);
        return res != 0 ? res : a.episodeNr.CompareTo(b.episodeNr);
    });
}
半岛未凉 2024-11-24 12:38:15

查看此链接中的示例
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx 创建一个IComparer

基本思想是

  • 实现,根据您的自定义比较条件返回 -1(小于)、0(等于)或 1(大于)。
  • 接下来将此类的实例传递给 List() 的 Sort 方法

另一个(a有点长)示例说明了这一点

Check out the example in this link
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx

The basic idea is to

  • Create a IComparer implementation that returns -1 (less than), 0 (equals) or 1 (greater than) based on your custom comparison criteria.
  • Next pass an instance of this class to the Sort method of your List()

Another (a bit long-drawn) example that illustrates this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文