在 C# 中使用 Comparer 按不同字段对 IEnumerable 进行排序

发布于 2024-08-23 07:13:59 字数 453 浏览 7 评论 0原文

我有一个对象列表,需要根据对象的三个不同属性进行排序。 示例

CLass Object1{ Property1 , Property2, Property3}

ListObj = IEnumerable<Object1>

Foreach ( item in ListObj){

    if (item.Property1 == true)
       item goes at top of list
    if(item.Property2 == true)
       item goes end of list
    if(item.Property3 == true)
        item can go anywhere.
}

最终列表应该是 Property1 = true 的对象,后跟 Property2 = true 的对象,然后是 Property3 = true 的对象

I have a list of an object which need to be sorted depending on three different properties of the object.
Example

CLass Object1{ Property1 , Property2, Property3}

ListObj = IEnumerable<Object1>

Foreach ( item in ListObj){

    if (item.Property1 == true)
       item goes at top of list
    if(item.Property2 == true)
       item goes end of list
    if(item.Property3 == true)
        item can go anywhere.
}

End list should be objects with Property1 = true followed by objects with Property2 = true followed by objects with Property3 = true

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

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

发布评论

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

评论(5

錯遇了你 2024-08-30 07:13:59

为什么不使用 LINQ?

var orderedList = 
   ListObj.OrderByDescending(x => x.Property1)
          .ThenByDescending(x => x.Property2);

Why not use LINQ?

var orderedList = 
   ListObj.OrderByDescending(x => x.Property1)
          .ThenByDescending(x => x.Property2);
梨涡少年 2024-08-30 07:13:59

您自己的标题已经说明了一切:实现自定义 IComparer 并将其传递给 OrderBy 扩展方法:

var orderedItems = ListObj.OrderBy(obj => obj, customComparer);

Your own title already says it all: implement a custom IComparer<Object1> and pass it to the OrderBy extension method:

var orderedItems = ListObj.OrderBy(obj => obj, customComparer);
萌化 2024-08-30 07:13:59

如果定义此类型,您可以让事情变得更简洁:

  public class ComparisonComparer<T> : IComparer<T>  
  {  
      private readonly Comparison<T> _comparison;  

      public ComparisonComparer(Comparison<T> comparison)  
      {  
          _comparison = comparison;  
      }  

      public int Compare(T x, T y)  
      {  
          return _comparison(x, y);  
      }  
 }  

它允许您使用 lambda 表达式定义与 LINQ 语句内联的比较。

You can make things neater for yourself if you define this type:

  public class ComparisonComparer<T> : IComparer<T>  
  {  
      private readonly Comparison<T> _comparison;  

      public ComparisonComparer(Comparison<T> comparison)  
      {  
          _comparison = comparison;  
      }  

      public int Compare(T x, T y)  
      {  
          return _comparison(x, y);  
      }  
 }  

Which lets you define the comparison inline with the LINQ statement using a lambda expression.

风蛊 2024-08-30 07:13:59

这应该提供所需的排序(根据代码,而不是下面的语句)。

ListObj.Where(x => x.Property1 == true)
       .Concat(ListObj.Where(x => x.Property1 == false && x.Property2 == false))
       .Concat(ListObj.Where(x => x.Property2 == true));

This should provide the required sorting (according to the code, not the statement below).

ListObj.Where(x => x.Property1 == true)
       .Concat(ListObj.Where(x => x.Property1 == false && x.Property2 == false))
       .Concat(ListObj.Where(x => x.Property2 == true));
才能让你更想念 2024-08-30 07:13:59

我认为您想要定义一个比较函数,您可以在其中确定列表中任意 2 个项目之间的排名。

    int CompareObject1(Object1 left, Object1 right)
    {
        // TODO: cases where your items are null

        // compare Property1 values
        if (left.Property1)
        {
            if (right.Property1)
            {
                // items at same rank
                return 0;
            }
            else
            {
                // left item is higher rank than right
                return -1;
            }
        }
        else if (right.Property1)
        {
            // right item is higher rank than left
            return 1;
        }

        // Property1 doesn't indicate position, move along
        // TODO: repeat for Property2

        // Property2 doesn't indicate position, move along
        // TODO: repeat for Property3

        // if we get here, no determination can 
        // be made/don't bother to move anything
        return 0;
    }

返回值指示左侧或右侧对象是否应排名较高,为 -1 或 1(或优先为 0)。只要确保您满足所有条件即可。

那么你可以使用这个,就像

List<Object1> foo = new List<Object1>() { <items...> };
foo.Sort(CompareObject1);

你的列表向后排列一样,我可能翻转了比较函数中的符号。您的排序规则是矛盾的,所以我让您对 Property2 和 Property3 进行排序。

i think you want to define a comparison function where you can determine rank between any 2 items in the list.

    int CompareObject1(Object1 left, Object1 right)
    {
        // TODO: cases where your items are null

        // compare Property1 values
        if (left.Property1)
        {
            if (right.Property1)
            {
                // items at same rank
                return 0;
            }
            else
            {
                // left item is higher rank than right
                return -1;
            }
        }
        else if (right.Property1)
        {
            // right item is higher rank than left
            return 1;
        }

        // Property1 doesn't indicate position, move along
        // TODO: repeat for Property2

        // Property2 doesn't indicate position, move along
        // TODO: repeat for Property3

        // if we get here, no determination can 
        // be made/don't bother to move anything
        return 0;
    }

the return value indicates if the left or right object should be ranked higher with -1 or 1 (or 0 for preference). just make sure you cover all your conditions.

then you can use this like

List<Object1> foo = new List<Object1>() { <items...> };
foo.Sort(CompareObject1);

if you're list ends up backwards, i probably flipped the signs in the compare function. your rules for sorting are contradictory so i'll let you sort Property2 and Property3.

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