Hey community members,
I'm just from an unsatisfactory search through Google. I'm trying at a program that automatically reads in numerical values dumped into a file by an Algebra software. Problem is, whenever I try to sort out numbers they are not appearing the way I want them to. These numbers are stored in an array of type array<String^>^. The reason for storing the as character arrays is because they are sometimes too large to be contained in the usual number types i.e. int, double etc, eg. <11797283819727347375364610680> (Not the largest BTW! I'm doing this for someone so I can't explain what they are used for!)

I tried to take advantage of the functionalities inherent in array but it is doing its doing it differently. A sample run gave me <"2","20","3","8">, instead of <"2","3","8","20">. Here's what worked a bit, but still isn't satisfactory to me.

void srt(array<String^>^ &l)
    bool sorted=false; int j=0, stop=(l->Length)*(l->Length)*(l->Length);
        for (int i=0;i<(l->Length-1);i++){
            String ^tmp; string str1, str2; 
            MarshalString(l[i],str1); MarshalString(l[i+1],str2);
            unsigned long dblval1, dblval2; char *stops="\0";
            dblval1=strtoul(str1.c_str(),&stops,8); dblval2=strtoul(str2.c_str(),&stops,8);

            if(dblval1 >dblval2){
                tmp=l[i]; l[i]=l[i+1]; l[i+1]=tmp;
            else sorted=true;

A sample run is giving me values such as <"0", "7", "12", "13", "4", "6"> which of course are not sorted! Please help

You're going to need to implement something like this: http://www.codeproject.com/KB/recipes/csnsort.aspx. It's probably better to make your numbers into a System::Collections::Generic::List<String^> which has a sort method in it. To use the sort like you want it to, you'll have to making your own class that inherits from IComparer and passing that object to the sort (see also http://msdn.microsoft.com/en-us/library/234b841s.aspx).

If you like your method better you can probably use Int32::TryParse to batch convert your String ^ arrays into int arrays. This all depends on how you will use the numbers downstream.

This article has been dead for over six months. Start a new discussion instead.