同时按两个参数对列表进行排序 C#

Sorting the list by two parameters simultaneously C#

我想按某些 fields/properties 结构对具有结构的列表进行排序。例如有一个结构:

public struct Some
{
    public int index;
    public DateTime date;
}

我是否有机会使用现有方法同时按两个参数对这样的结构进行排序?比如说,在此类结构列表的顶部放置那些将具有最新 date 和最大 index 的结构。是否可以同时考虑这两个参数进行排序?

是的,您可以使用 LINQ

使用 OrderBy 方法

OrderByThenBy 是您需要的方法。

示例:

list.OrderBy(x => x.index).ThenBy(x => x.date)

如果你加上Descending你也可以反过来排序。

使用自定义排序比较函数。

示例 1:

    public struct MyItem
    {
        public int index;
        public DateTime date;
    }
    class Program
    {
        static void Main(string[] args)
        {
            var items = new MyItem[]{
                new MyItem{index=9, date=DateTime.Now},
                new MyItem{index=4, date=DateTime.Now},
                new MyItem{index=3, date=DateTime.Now},
                new MyItem{index=5, date=DateTime.Now},
                new MyItem{index=5, date=DateTime.Now + TimeSpan.FromDays(1)},
                new MyItem{index=6, date=DateTime.Now},
            };
            // sort by index, if equal then sort by date
            Array.Sort(items, (x, y) =>
            {
                if (x.index == y.index)
                    return x.date.CompareTo(y.date);
                return x.index.CompareTo(y.index);
            });

            foreach (var item in items)
                Console.WriteLine($"{item.index} {item.date}");
        }
    }

示例 2:

var items = new List<MyItem>{
    new MyItem{index=9, date=DateTime.Now},
    new MyItem{index=4, date=DateTime.Now},
    new MyItem{index=3, date=DateTime.Now},
    new MyItem{index=5, date=DateTime.Now},
    new MyItem{index=5, date=DateTime.Now + TimeSpan.FromDays(1)},
    new MyItem{index=6, date=DateTime.Now},
};
// sort by index, if equal then sort by date
items.Sort((x, y) => x.index.CompareTo(y.index) == 0 ? x.date.CompareTo(y.date) : x.index.CompareTo(y.index));

示例 3: Linq

            var items = new List<MyItem>{
                new MyItem{index=9, date=DateTime.Now},
                new MyItem{index=4, date=DateTime.Now},
                new MyItem{index=3, date=DateTime.Now},
                new MyItem{index=5, date=DateTime.Now},
                new MyItem{index=5, date=DateTime.Now + TimeSpan.FromDays(1)},
                new MyItem{index=6, date=DateTime.Now},
            };
            // sort by index, if equal then sort by date
            var newItems = items.OrderBy(x => x.index).ThenBy(x => x.date);

您可以使用 LINQ 轻松地按列表中包含的值对列表中的项目进行排序。

using System.Linq;

...

myStructs.OrderBy(s => s.index).ThenBy(s => s.date)

将按索引排列所有内容。具有相同索引的项目将按日期排序。

如果您想 Sort 到位 而您不想实施 IComparable<Some>IComparer<Some>:

List<Some> myList = ...

...

myList.Sort((left, right) => {
  // latest dates on the top (note - for descending sorting)
  int result = -left.date.CompareTo(right.date);

  // on tie when left and right have the same date we compare indexes
  return result == 0 
    ? -left.index.CompareTo(right.index)
    :  result; 
});

如果你有几个片段,你想以这种方式对列表进行排序,你可以实现一个比较器:

public sealed class SomeComparer : IComparer<Some> { 
  public int Compare(Some left, Some right) {
    // latest dates on the top (note - for descending sorting)
    int result = -left.date.CompareTo(right.date);

    // on tie when left and right have the same date we compare indexes
    return result == 0 
      ? -left.index.CompareTo(right.index)
      :  result;
  }
} 

然后每当你想对列表进行排序时:

myList.Sort(new SomeComparer());

最后,如果这是您想要对 Some 项进行排序的唯一顺序,您可以使 Sort 具有可比性:

public struct Some : IComparable<Some>
{
    public int index;
    public DateTime date;

    //TODO: implement Equals and GetHashCode 

    public bool CompareTo(Some other) {
      int result = -date.CompareTo(other.date);

      return result == 0 
        ? -index.CompareTo(other.index)
        : result;
    }
}

你可以把

myList.Sort();