[英]Sort a list alphabetically
I have the following class:我有以下课程:
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
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?
我可以用 LINQ 做到这一点吗? Sorry but I am not very familiar with LINQ I have just heard a suggestion that I could use that.
抱歉,我对 LINQ 不是很熟悉,我刚刚听到一个建议,我可以使用它。
You can sort a list in-place just by calling List<T>.Sort
:您可以通过调用
List<T>.Sort
就地对列表进行排序:
list.Sort();
That will use the natural ordering of elements, which is fine in your case.这将使用元素的自然顺序,这在您的情况下很好。
EDIT: Note that in your code, you'd need编辑:请注意,在您的代码中,您需要
_details.Sort();
as the Sort
method is only defined in List<T>
, not IList<T>
.因为
Sort
方法仅在List<T>
定义,而不是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.如果您需要从外部对其进行排序,而您无法将其作为
List<T>
(您不应该将其转换为List<T>
部分是一个实现细节),您需要执行多一点工作。
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.我不知道.NET 中有任何基于
IList<T>
的就地排序,现在我想到它有点奇怪。 IList<T>
provides everything you'd need, so it could be written as an extension method. IList<T>
提供了您需要的一切,因此可以将其编写为扩展方法。 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.换句话说,复制,就地排序,然后将排序后的列表复制回来。
You can use LINQ to create a new list which contains the original values but sorted:您可以使用 LINQ 创建一个包含原始值但已排序的新列表:
var sortedList = list.OrderBy(x => x).ToList();
It depends which behaviour you want.这取决于您想要哪种行为。 Note that your shuffle method isn't really ideal:
请注意,您的 shuffle 方法并不理想:
Random
within the method runs into some of the problems shown hereRandom
遇到了这里显示的一些问题val
inside the loop - you're not using that default valueval
- 您没有使用该默认值Count
property when you know you're working with an IList<T>
IList<T>
时,使用Count
属性更为惯用for
loop is simpler to understand than traversing the list backwards with a while
loopfor
循环比使用while
循环向后遍历列表更容易理解There are other implementations of shuffling with Fisher-Yates on Stack Overflow - search and you'll find one pretty quickly.在 Stack Overflow 上还有使用 Fisher-Yates 进行改组的其他实现 - 搜索,您会很快找到一个。
There are two ways:有两种方式:
Without LINQ: yourList.Sort();
没有 LINQ:
yourList.Sort();
With LINQ: yourList.OrderBy(x => x).ToList()
使用 LINQ:
yourList.OrderBy(x => x).ToList()
You will find more information in: https://www.dotnetperls.com/sort您可以在以下位置找到更多信息: https : //www.dotnetperls.com/sort
其他方式
_details.Sort((s1, s2) => s1.CompareTo(s2));
您应该能够在 LINQ 中使用OrderBy
......
var sortedItems = myList.OrderBy(s => s);
What is wrong with List<T>.Sort()
? List<T>.Sort()
什么问题?
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort#overloads https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort#overloads
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.