C# 排序列表,逻辑比较

发布于 2024-09-08 11:21:28 字数 353 浏览 2 评论 0原文

我有以下问题 我有一个包含字符串的列表,例如 (100_1, 100_2 ...., 100_10)

我用以下代码对列表进行排序,

extraImgsRaw.Sort((photo1, photo2) => photo1.CompareTo(photo2));

结果是: 100_1, 100_10, 100_2, 100_3 等等,

我想要的结果是一个逻辑比较 100_1、100_2,然后是 100_10,所以我更喜欢 自然数字排序不是字母排序。 我是否需要编写自己的比较类来实现 ICompare 接口,或者 LINQ 中有一个构建方法可以做到这一点?

先感谢您

i have the following problem
I have a list with strings for example (100_1, 100_2 .... , 100_10)

I sort the list with following code

extraImgsRaw.Sort((photo1, photo2) => photo1.CompareTo(photo2));

the result of this is : 100_1, 100_10, 100_2, 100_3 and so on

the result that I want is a logical compare like 100_1, 100_2 and then 100_10 so I prefer a
Natural numeric sort not a Alphabetic sort.
Do I need to write my own compare class that implements the ICompare interface or there is a build method in LINQ that does that?

thank you in advance

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

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

发布评论

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

评论(2

峩卟喜欢 2024-09-15 11:21:28

没有任何内置内容,但如果数据与您的问题中所示完全相同,那么敲出 Comparison为您执行此操作:

extraImgsRaw.Sort((x, y) =>
                  {
                      // error checking etc removed for brevity
                      int[] xi = x.Split('_').Select(int.Parse).ToArray();
                      int[] yi = y.Split('_').Select(int.Parse).ToArray();

                      int c = xi[0].CompareTo(yi[0]);
                      return (c != 0) ? c : xi[1].CompareTo(yi[1]);
                  });

There's nothing built-in, but if the data is exactly as shown in your question then it shouldn't be too difficult to knock up a Comparison<T> to do this for you:

extraImgsRaw.Sort((x, y) =>
                  {
                      // error checking etc removed for brevity
                      int[] xi = x.Split('_').Select(int.Parse).ToArray();
                      int[] yi = y.Split('_').Select(int.Parse).ToArray();

                      int c = xi[0].CompareTo(yi[0]);
                      return (c != 0) ? c : xi[1].CompareTo(yi[1]);
                  });
差↓一点笑了 2024-09-15 11:21:28

拆分和比较元素,

这是我为“版本”编写的一个。

  /// <summary>
  /// Only works for version numbers in the form a ( . b ( . c ( . d )? )? )?
  /// </summary>
  public class VersionComponents : IComparable<VersionComponents>
  {
    readonly int[] components;

    int[] GetComponents(string cpnumber)
    {
      var tokens = cpnumber.Split(".".ToCharArray(), 
                                   StringSplitOptions.RemoveEmptyEntries);
      return tokens.Select(x => Convert.ToInt32(x)).ToArray();
    }

    public VersionComponents(string cpnumber)
    {
      components = GetComponents(cpnumber);
    }

    public int this[int index]
    {
      get { return components.Length > index ? components[index] : 0; }
    }

    public int CompareTo(VersionComponents other)
    {
      for (int i = 0; i < components.Length || 
                      i < other.components.Length; i++)
      {
        var diff = this[i].CompareTo(other[i]);
        if (diff != 0)
        {
          return diff;
        }
      }

      return 0;
    }
  }

Split and compare elements,

Here is one I wrote for 'versions'.

  /// <summary>
  /// Only works for version numbers in the form a ( . b ( . c ( . d )? )? )?
  /// </summary>
  public class VersionComponents : IComparable<VersionComponents>
  {
    readonly int[] components;

    int[] GetComponents(string cpnumber)
    {
      var tokens = cpnumber.Split(".".ToCharArray(), 
                                   StringSplitOptions.RemoveEmptyEntries);
      return tokens.Select(x => Convert.ToInt32(x)).ToArray();
    }

    public VersionComponents(string cpnumber)
    {
      components = GetComponents(cpnumber);
    }

    public int this[int index]
    {
      get { return components.Length > index ? components[index] : 0; }
    }

    public int CompareTo(VersionComponents other)
    {
      for (int i = 0; i < components.Length || 
                      i < other.components.Length; i++)
      {
        var diff = this[i].CompareTo(other[i]);
        if (diff != 0)
        {
          return diff;
        }
      }

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