我有以下参考结构:
public ref struct WordCollection
{
public int length;
public int currentIndex;
public Span<byte> buffer;
public Span<int> indexes;
public Span<int> wordSize;
public Span<int> wordLocation;
public Span<int> wordCount;
}
我想indexes
通过wordCount
比较wordCount[indexes[i]]
来排序wordCount[indexes[j]]
第一个想法是使用Sort<T>(Span<T>, Comparison<T>)
但是这样的事情是行不通的
void DoStuff()
{
WordCollection wordCollection = new WordCollection
{
length = 0,
currentIndex = 0,
buffer = stackalloc byte[MaxBufferSize],
indexes = stackalloc int[MaxItems],
wordSize = stackalloc int[MaxCollectionSize],
wordLocation = stackalloc int[MaxCollectionSize],
wordCount = stackalloc int[MaxCollectionSize]
};
...................
static int Compare(int x, int y)
{
return wordCollection.wordCount[x].CompareTo(wordCollection.wordCount[y]);
}
wordCollection.indexes.Sort(Compare);
}
错误是:不能在匿名方法、lambda 表达式或查询表达式中使用 ref local 'wordCollection'
还有,public static void Sort<TKey,TValue,TComparer> (this Span<TKey> keys, Span<TValue> items, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<TKey>;
但我也不能使用它,因为 ref structs 不能实现接口。
那么,有没有一种方法可以使用框架提供indexes
的wordCounts[indexes[i]]
Sort 方法进行排序,而无需编写自己的排序代码?