如何只对列表/ arraylist中的前6个元素进行排序? [英] How to I sort only the first 6 elements in a list/arraylist?

查看:99
本文介绍了如何只对列表/ arraylist中的前6个元素进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含8个元素的列表,我想只排序/排序前6个元素,并保留最后两个元素?

I have a list with 8 elements and I want to sort/order only the first 6 elements, and leave the last two as they are?

List<string> lst = new List<string>();
Random rnd = new Random();

for (int i = 0; i < 8; i++)
{
    char let;
    do
    {
        int num = rnd.Next(0, 26);
        let = (char)('A' + num);
    } while (lst.Contains(let.ToString()));

    lst.Add(let.ToString());
}

//I want to sort the first 6 elements here

ListViewItem lvi = new ListViewItem(lst[0]);
for (int i = 1; i < lst.Count; i++)
{
    lvi.SubItems.Add(lst[i]);
}
lvwCheckOut.Items.Add(lvi);



如果列表中的字母是:DLAZRUWG

我希望我的输出为:ADLRUZWG



我尝试过:



我已经尝试了很多东西,如果我做两个列表,我会得到我想要的结果。列表1有6个元素,只需使用lst1.Sort();然后用2个元素列出2并保持随机。


If the letters in the list are: D L A Z R U W G
I want my output to be: A D L R U Z W G

What I have tried:

I have tried a lot of thing, I get the results I want if I do two lists. List 1 with 6 elements and just use "lst1.Sort();" then list 2 with 2 elements and leave it random.

推荐答案

看这里:部分排序 - C#是否等同于C ++ std :: partial_sort? - 堆栈溢出 [ ^ ]。



第二个答案(Lee)只允许您对前6个项目进行排序。



另请参阅MSDN文档: List(T).Sort方法( Int32,Int32,IComparer(T))(System.Collections.Generic) [ ^ ]
Look here: partial sort - Is there a C# equivalent to C++ std::partial_sort? - Stack Overflow[^].

The second answer (Lee) would allows you to sort only the first 6 items.

See also MSDN documentation: List(T).Sort Method (Int32, Int32, IComparer(T)) (System.Collections.Generic)[^]


虽然Philippe Mori(可能)根据Alan N.在他的评论中提出的解决方案将解决方案与此相关联,确实,得到正确的结果,你真的需要让解决方案变得复杂吗?



为什么不简单地先生成六个随机字母,然后,你可以轻松对它们进行排序,然后生成两个随机字母?
While the solution linked to here by Philippe Mori based (probably) on the suggestion by Alan N. in his comment will, indeed, get the correct result, do you really need to make the solution this complex ?

Why not simply first generate six random letters, and, then, you can easily sort them, and then generate two random letters ?
private static Random randGenerator = new Random(DateTime.Now.Millisecond);

private static string Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

public IEnumerable<char> GetRandomChars(int count, bool useuppercase = false)
{
    for (int i = 0; i < count; i++)
    {
        yield return
            (useuppercase)
                ? Letters[randGenerator.Next(0, 52)]
                : Letters[randGenerator.Next(0, 26)];
    }
}

//使用示例:在某些Method或EventHandler中执行

// use example: execute in some Method or EventHandler

var firstsix = GetRandomChars(6, false).ToList();
firstsix.Sort();

var lasttwo = GetRandomChars(2, false);

var result = firstsix.Concat(lasttwo);

注意:



1.这段代码可以追溯到Linq之前的许多年,并且在Linq的第一个版本出现后更新了,为了方便起见返回了IEnumerable结果。



2.请注意,在调用'Sort运算符之前,必须评估实际结果包含在var'firstsix中的IEnumerable,并注意'Sort转换其参数但不返回任何内容(即'void)。



3.最终的var'结果将包含一个IEnumerable< char>,请记住延迟评估有效这里;这是我最近试图解释的内容:[ ^ ]。



4.注意这里使用的排序运算符不是Linq;它是由System.Collections.Generic提供的方法,它随C#2.0到达

Note:

1. this code goes back many years to pre-Linq days, and was updated, after the first version of Linq came along, to return an IEnumerable result for convenience.

2. note that you have to "evaluate to an actual result" the IEnumerable contained in the var 'firstsix before you can call the 'Sort operator, and note that 'Sort transforms its argument but returns nothing (i.e., 'void).

3. The final var 'result will contain an IEnumerable<char>, so keep in mind that "deferred evaluation" is in effect here; that's something I recently tried to explain here: [^].

4. note the 'Sort operator used here is not Linq; it's a method provided by System.Collections.Generic, which arrived with C#2.0


试试这个



try this

var first6Sorted = lst.Take(6).OrderBy(k => k).ToList();  // First 6 chars sorted
    var last2UnSorted = lst.Skip(6).ToList(); // last 2 chars unsorted
    lst = first6Sorted;
    lst.AddRange(last2UnSorted);  // merging both the collection


这篇关于如何只对列表/ arraylist中的前6个元素进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆