4 回答

TA貢獻1784條經驗 獲得超7個贊
您可能希望讓您的類成為Comparable而不是Comparator
public class Card : IComparable<Card>
{
? ? public string ID;
? ? public string Name;
? ? public int CompareTo(Card other)?
? ? {
? ? ? ? if (null == other)
? ? ? ? ? ? return 1;
? ? ? ? // string.Compare is safe when Id is null?
? ? ? ? return string.Compare(this.Id, other.Id);
? ? }
}
然后
List<Card> myList = ...
myList.Sort();
編輯:如果您想要有多個標準可供選擇,則必須將多個比較器實現為單獨的類,例如
public sealed class CardByIdComparer : IComparer<Card>?
{
? ? public int Compare(Card x, Card y)?
? ? {
? ? ? ? if (object.ReferenceEquals(x, y))
? ? ? ? ? ? return 0;
? ? ? ? else if (null == x)
? ? ? ? ? ? return -1;
? ? ? ? else if (null == y)
? ? ? ? ? ? return 1;
? ? ? ? else
? ? ? ? ? ? return string.Compare(x.Id, y.Id);
? ? }
}
并在排序時提供所需的:
List<Card> myList = ...
myList.Sort(new CardByIdComparer());
編輯2:(受到消費者圖書館的啟發)。如果您想將多個比較器合并為一個(即使用comparer1, on tie -comparer2等)
public sealed class ComparerCombined<T> : IComparer<T> {
? private IComparer<T>[] m_Comparers;
? public ComparerCombined(params IComparer<T>[] comparers) {
? ? if (null == comparers)
? ? ? throw new ArgumentNullException(nameof(comparers));
? ? m_Comparers = comparers
? ? ? .Select(item => item == null ? Comparer<T>.Default : item)
? ? ? .Where(item => item != null)
? ? ? .Distinct()
? ? ? .ToArray();
? }
? public int Compare(T x, T y) {
? ? if (object.ReferenceEquals(x, y))
? ? ? return 0;
? ? else if (null == x)
? ? ? return -1;
? ? else if (null == y)
? ? ? return 1;
? ? foreach (var comparer in m_Comparers) {
? ? ? int result = comparer.Compare(x, y);
? ? ? if (result != 0)
? ? ? ? return result;
? ? }
? ? return 0;
? }
}
用法:
myList.Sort(new ComparerCombined(
? new CardByIdComparer(),? ?// Sort By Id
? new CardByNameComparer()? // On tie (equal Id's) sort by name
));

TA貢獻1840條經驗 獲得超5個贊
最簡單的方法你可以使用 Linq:
List<Card> objSortedList = objListObject.OrderBy(o=>o.ID).ToList();
或者
List<Card> objSortedList = objListObject.OrderByDescending(o=>o.ID).ToList();

TA貢獻1777條經驗 獲得超10個贊
您需要實現 IComparer
public int Compare(Card card1, Card card2)
{
if (card1.ID > card2.ID)
return 1; //move card1 up
if (card2.ID < card1.ID)
return -1; //move card2 up
return 0; //do nothing
}

TA貢獻1859條經驗 獲得超6個贊
IComparer<T>
在此示例中,比較方法用于字符串IComparer<T>
?,但您也可以將其用于 ID(int)。
using System;?
using System.Collections.Generic;?
class GFG : IComparer<string>?
{?
? ? public int Compare(string x, string y)?
? ? {?
? ? ? ? if (x == null || y == null)?
? ? ? ? {?
? ? ? ? ? ? return 0;?
? ? ? ? }?
? ? ? ? // "CompareTo()" method?
? ? ? ? return x.CompareTo(y);?
? ? }?
}?
public class geek?
{?
? ? public static void Main()?
? ? {?
? ? ? ? List<string> list1 = new List<string>();?
? ? ? ? // list elements?
? ? ? ? list1.Add("C++");?
? ? ? ? list1.Add("Java");?
? ? ? ? list1.Add("C");?
? ? ? ? list1.Add("Python");?
? ? ? ? list1.Add("HTML");?
? ? ? ? list1.Add("CSS");?
? ? ? ? list1.Add("Scala");?
? ? ? ? list1.Add("Ruby");?
? ? ? ? list1.Add("Perl");?
? ? ? ? int range = 4;?
? ? ? ? GFG gg = new GFG();?
? ? ? ? Console.WriteLine("\nSort a range with comparer:");?
? ? ? ? // sort the list within a??
? ? ? ? // range of index 1 to 4?
? ? ? ? // where range = 4?
? ? ? ? list1.Sort(1, range, gg);?
? ? ? ? Console.WriteLine("\nBinarySearch and Insert Dart");?
? ? ? ? // Binary Search and storing??
? ? ? ? // index value to "index"?
? ? ? ? int index = list1.BinarySearch(0, range,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "Dart", gg);?
? ? ? ? if (index < 0)?
? ? ? ? {?
? ? ? ? ? ? list1.Insert(~index, "Dart");?
? ? ? ? ? ? range++;?
? ? ? ? }?
? ? }?
}?
- 4 回答
- 0 關注
- 205 瀏覽
添加回答
舉報