简体   繁体   English

C#数组编号排序

[英]C# Array number sorting

I have an array of numbers jumbled up from 0-9. 我有一个从0-9混杂的数字数组。

How do I sort them in ascending order? 如何按升序对它们进行排序?

Array.Sort doesn't work for me. Array.Sort对我不起作用。 Is there another way to do it? 还有另一种方法吗?

Thanks in advance. 提前致谢。

EDIT: Array.Sort gives me this error. 编辑:Array.Sort给我这个错误。

Argument 1: cannot convert from 'string' to 'System.Array' 参数1:无法从“字符串”转换为“ System.Array”

Right now it gives me this output: 现在,它给了我这个输出:

0) VersionInfo.xml 0)VersionInfo.xml

2) luizafroes_singapore2951478702.xml 2)luizafroes_singapore2951478702.xml

3) virua837890738.xml 3)virua837890738.xml

4) darkwizar9102314425644.xml 4)darkwizar9102314425644.xml

5) snarterz_584609551.xml 5)snarterz_584609551.xml

6) alysiayeo594136055.xml 6)alysiayeo594136055.xml

1) zan-n2306499277.xml 1)zan-n2306499277.xml

7) zhangliyi_memories932668799030.xml 7)zhangliyi_memories932668799030.xml

8) andy_tan911368887723.xml 8)andy_tan911368887723.xml

9) config.xml 9)config.xml

k are the numbers from 0-9 k是0-9的数字

                    string[] valnames = rk2.GetValueNames();

                foreach (string k in valnames)
                {
                    if (k == "MRUListEx")
                    {
                        continue;
                    }

                    Byte[] byteValue = (Byte[])rk2.GetValue(k);

                    UnicodeEncoding unicode = new UnicodeEncoding();
                    string val = unicode.GetString(byteValue);

                    Array.Sort(k); //Error here
                    richTextBoxRecentDoc.AppendText("\n" + k + ") " + val + "\n");
                }

Your problem is that k is not an Array but a string ! 您的问题是k不是数组而是字符串!

I have the feeling that this is what you want to do : 我感觉这就是您想要做的:

string[] valnames = rk2.GetValueNames();
valnames = valnames.OrderBy (s => int.Parse(s)).ToArray();

for (int i= 0 ; i < balnames.Lenght ; i++)
{
    k = valenames[i];
    if (k == "MRUListEx")
    {
        continue;
    }
    Byte[] byteValue = (Byte[])rk2.GetValue(k);

    UnicodeEncoding unicode = new UnicodeEncoding();
    string val = unicode.GetString(byteValue);

    richTextBoxRecentDoc.AppendText("\n" + i + ") " + val + "\n");
}

您确定具有整数数组还是存储了System.Object数组,在这种情况下,前导空间会出现问题。

You are trying to sort a String. 您正在尝试对字符串进行排序。 That's not possible. 那不可能

This code will give you the output you want: 此代码将为您提供所需的输出:

string[] valnames = rk2.GetValueNames();

for (int i = valnames.Length - 1; i >= 0; i--)
{
    string k = valnames[i];
    if (k == "MRUListEx")
        continue;

    Byte[] byteValue = (Byte[])rk2.GetValue(k);
    UnicodeEncoding unicode = new UnicodeEncoding();
    string val = unicode.GetString(byteValue);

    richTextBoxRecentDoc.AppendText("\n" + k + ") " + val + "\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM