athgap
athgap

Reputation: 165

C# Array number sorting

I have an array of numbers jumbled up from 0-9.

How do I sort them in ascending order?

Array.Sort doesn't work for me. Is there another way to do it?

Thanks in advance.

EDIT: Array.Sort gives me this error.

Argument 1: cannot convert from 'string' to 'System.Array'

Right now it gives me this output:

0) VersionInfo.xml

2) luizafroes_singapore2951478702.xml

3) virua837890738.xml

4) darkwizar9102314425644.xml

5) snarterz_584609551.xml

6) alysiayeo594136055.xml

1) z-a-n-n2306499277.xml

7) zhangliyi_memories932668799030.xml

8) andy_tan911368887723.xml

9) config.xml

k are the numbers from 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");
                }

Upvotes: 0

Views: 2122

Answers (3)

VdesmedT
VdesmedT

Reputation: 9113

Your problem is that k is not an Array but a string !

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");
}

Upvotes: 4

Shadow Wizard
Shadow Wizard

Reputation: 66399

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");
}

Upvotes: 0

Raj
Raj

Reputation: 1770

Are you sure you have an array in integers or have you stored an array of System.Object, in which case you'll have problems with leading space.

Upvotes: 0

Related Questions