我有以下类:
class Detail
{
public Detail()
{
_details = new List<string>();
}
public IList<string> Details { get { return _details; } }
private readonly List<string> _details;
}
目前我使用以下方法随机排序类:
void ShuffleGenericList<T>(IList<T> list)
{
//generate a Random instance
var rnd = new Random();
//get the count of items in the list
var i = list.Count();
//do we have a reference type or a value type
T val = default(T);
//we will loop through the list backwards
while (i >= 1)
{
//decrement our counter
i--;
//grab the next random item from the list
var nextIndex = rnd.Next(i, list.Count());
val = list[nextIndex];
//start swapping values
list[nextIndex] = list[i];
list[i] = val;
}
}
我希望做的是按字母顺序对详细内容进行排序。
例如,如果内容如下:
[0] a
[1] d
[2] b
我希望能够运行此方法并将它们排序为:
[0] a
[1] b
[2] d
有人知道如何简单地实现这个吗?请注意,列表通常只有不到十个条目。我可以用LINQ来做吗?抱歉,我对LINQ并不是很熟悉,我只是听说可以用它。
_details.Sort()
代替 - 因为_details
被声明为List<string>
而不是IList<string>
。Sort
仅在List<T>
上声明,而不是在IList<T>
上声明。 - Jon Skeet