0

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");
                }
5
  • 3
    Array.Sort should work. Why is it not working? Could you extend the question please? Commented Dec 22, 2010 at 10:38
  • Just edited. How do I solve the problem? Commented Dec 22, 2010 at 10:39
  • 3
    What is the code you are running to create and sort the array? Commented Dec 22, 2010 at 10:40
  • "Argument 1: cannot convert from 'string' to 'System.Array'". You're probably not using an array of integers. int[] myNums = {3, 2, 8, 5, 4, 9}; Commented Dec 22, 2010 at 10:41
  • Can you post the code, in addition to the error message? It shouldn't be more than 2 lines. Commented Dec 22, 2010 at 10:41

3 Answers 3

4

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");
}
Sign up to request clarification or add additional context in comments.

7 Comments

So how should I solve my problem? It gives me the numbers 0-9 though.
I'm not very sure of what you are trying to do but probably a Array.Sort(valnames) before the loop will do what you need isn't it ?
You can only use Array.Sort to sort arrays. A string is not an array. You need to re-think what the code is meant to be doing.
You should solve the problem by calling Array.Sort with an array, not with a string! (e.g. Array.Sort(byteValue);). Whatever it is you want to sort, it's almost certainly not k!
The output will be 1 10 11 12 13 14 15 16 2 21 22 23 24 25. How do I arrange it in ascending order?
|
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.