How to compare unordered string arrays

I have a string array that I make ahead of time that I need to compare to another string array that is prepared by my GAUSS program. The problem is that my program will create the string array in an arbitrary order. For a simple illustration, I may start with:

string sa_1 = { "AGE", "INCOME", "RISK_TOLERANCE", "QALY" };

and need to compare it to another string array that might be in any order. What is the simplest way to do this?

1 Answer



0



accepted

The simplest way to make this comparison is by first sorting the string array created by your program and then doing a direct compare of the two string arrays like this:

string sa_1 = { "AGE", "INCOME", "QALY", "RISK_TOL" };
string sa_2 = { "QALY", "INCOME", "RISK_TOL", "AGE" };

//sort string array based upon the first (and only) column
sa_2 = sortcc(sa_2, 1);

//compare the sorted 'sa_2' with the original string array
//using the string comparison operator $==
if (sa_1 $== sa_2);
   print "sa_1 = " sa_1;
   print "sa_2 = " sa_2;
else;
   print "comparison failed";
endif;

This short program snippet should return:

sa_1 = 
             AGE 
          INCOME 
            QALY 
        RISK_TOL 
sa_2 = 
             AGE 
          INCOME 
            QALY 
        RISK_TOL 

aptech

1,773

Your Answer

1 Answer

0
accepted

The simplest way to make this comparison is by first sorting the string array created by your program and then doing a direct compare of the two string arrays like this:

string sa_1 = { "AGE", "INCOME", "QALY", "RISK_TOL" };
string sa_2 = { "QALY", "INCOME", "RISK_TOL", "AGE" };

//sort string array based upon the first (and only) column
sa_2 = sortcc(sa_2, 1);

//compare the sorted 'sa_2' with the original string array
//using the string comparison operator $==
if (sa_1 $== sa_2);
   print "sa_1 = " sa_1;
   print "sa_2 = " sa_2;
else;
   print "comparison failed";
endif;

This short program snippet should return:

sa_1 = 
             AGE 
          INCOME 
            QALY 
        RISK_TOL 
sa_2 = 
             AGE 
          INCOME 
            QALY 
        RISK_TOL 

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.