我想按結構的某些欄位/屬性對具有結構的串列進行排序。例如有一個結構:
public struct Some
{
public int index;
public DateTime date;
}
我是否有機會使用現有方法同時通過兩個引數對這樣的結構進行排序?比如說,在此類結構串列的頂部放置那些將具有最新date和最大的index. 是否可以同時考慮這兩個引數進行排序?
uj5u.com熱心網友回復:
是的,您可以使用LINQ
使用OrderBy方法
OrderBy并且ThenBy是您需要的方法。
例子:
list.OrderBy(x => x.index).ThenBy(x => x.date)
如果你添加降序,你也可以反過來排序。
uj5u.com熱心網友回復:
使用自定義排序比較功能。
示例 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);
uj5u.com熱心網友回復:
如果您想Sort 就位并且不想實施IComparable<Some>or 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();
uj5u.com熱心網友回復:
您可以使用 LINQ 輕松地按串列中包含的值對串列中的專案進行排序。
using System.Linq;
...
myStructs.OrderBy(s => s.index).ThenBy(s => s.date)
將按索引排列所有內容。具有相同索引的專案將按日期排序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/470015.html
