排序名单按字母顺序 [英] Sort a list alphabetically
问题描述
我有以下类:
class Detail
{
public Detail()
{
_details = new List<string>();
}
public IList<string> Details { get { return _details; } }
private readonly List<string> _details;
}
目前我排序的类随机使用以下内容:
Currently I sort the class randomly using the following:
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;
}
}
我想这样做是按字母顺序的细节内容进行排序。
What I would like to do is to sort the contents of details in alphabetic order.
所以,举例来说,如果内容是这样的:
So for example if the contents look like this:
[0] a
[1] d
[2] b
我希望能够运行这个方法,并将它们整理成:
I want to be able to run this method and have them sorted into:
[0] a
[1] b
[2] d
有谁知道一个简单的方法来做到这一点?注意,列表通常具有在其中不到10项。我能做到这一点的LINQ?很抱歉,但我不是很熟悉,LINQ我刚才听说,我可以用一个建议。
Does anyone know of a simple way to do this? Note that the lists usually have less than ten entries in them. Can I do this with LINQ? Sorry but I am not very familiar with LINQ I have just heard a suggestion that I could use that.
推荐答案
您可以的只是通过调用<一种就地列表中的 href="http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx"><$c$c>List<T>.Sort$c$c>:
list.Sort();
这将使用元素的自然顺序,这是很好的,你的情况。
That will use the natural ordering of elements, which is fine in your case.
编辑:请注意,在code,你需要
Note that in your code, you'd need
_details.Sort();
为排序
方法仅在定义列表&LT; T&GT;
,不是的IList&LT ; T&GT;
。如果您需要从外部排序,你没有访问到它作为一个名单,其中,T&GT;
(你不应该将它转换为列表&LT; T&GT;
部分是一个实现细节),你需要做更多的工作。
as the Sort
method is only defined in List<T>
, not IList<T>
. If you need to sort it from the outside where you don't have access to it as a List<T>
(you shouldn't cast it as the List<T>
part is an implementation detail) you'll need to do a bit more work.
我不的知道的任何的IList&LT; T&GT;
为主的就地排序在.NET中,这是有点奇怪,现在我想起来了。 的IList&LT; T&GT;
提供一切你所需要的,所以它的可以的写成扩展方法。还有,如果你身边要使用其中的一个批次快速排序的实现。
I don't know of any IList<T>
-based in-place sorts in .NET, which is slightly odd now I come to think of it. IList<T>
provides everything you'd need, so it could be written as an extension method. There are lots of quicksort implementations around if you want to use one of those.
如果你不关心有点低效率的,你总是可以使用:
If you don't care about a bit of inefficiency, you could always use:
public void Sort<T>(IList<T> list)
{
List<T> tmp = new List<T>(list);
tmp.Sort();
for (int i = 0; i < tmp.Count; i++)
{
list[i] = tmp[i];
}
}
在换句话说,复印,排序到位,然后复制排序列表后面。
In other words, copy, sort in place, then copy the sorted list back.
您可以使用LINQ创建的新列表中包含原始值,但排序:
You can use LINQ to create a new list which contains the original values but sorted:
var sortedList = list.OrderBy(x => x).ToList();
这取决于你想要的行为。请注意,您的洗牌方法是不是真的理想:
It depends which behaviour you want. Note that your shuffle method isn't really ideal:
- 创建一个新的
随机
中的方法运行到一些问题的显示这里 - 您可以声明
VAL
里面的循环 - 你不是的使用的是默认值 - 这是更地道使用
计数
属性,当您的知道的你正在使用的的IList&LT; T&GT;
- 要我的脑海里,一个
为
循环更易于理解比遍历列表向后与,而
循环
- Creating a new
Random
within the method runs into some of the problems shown here - You can declare
val
inside the loop - you're not using that default value - It's more idiomatic to use the
Count
property when you know you're working with anIList<T>
- To my mind, a
for
loop is simpler to understand than traversing the list backwards with awhile
loop
有与堆栈溢出费雪耶茨洗牌的其他实现 - 搜索,你会很快找到一个pretty的
There are other implementations of shuffling with Fisher-Yates on Stack Overflow - search and you'll find one pretty quickly.
这篇关于排序名单按字母顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!