Problem on Sorting Strings

Hi, I have this problem on sorting strings. I want to sort the Strings in ascending order. Here are my strings:
A,1111
B,3333
C,2222
D,3333
A,3333
B,2222
C,1111
D,2222
A,2222
B,1111
C,3333
D,1111
I am getting them from a text file. How can I make it look like this:
A,1111
A,2222
A,3333
B,1111
B,2222
B,3333
C,1111
C,2222
C,3333
D,1111
D,2222
D,3333
I have a code already but my results are like this:

A,111 --->>> Sorted Elements - Provision for FIRST PASS
A,333 --->>> Sorted Elements - Provision for FIRST PASS
A,222 --->>> Sorted Elements - Provision for FIRST PASS
B,333 --->>> Sorted Elements - Provision for FIRST PASS
B,222 --->>> Sorted Elements - Provision for FIRST PASS
B,111 --->>> Sorted Elements - Provision for FIRST PASS
C,111 --->>> Sorted Elements - Provision for FIRST PASS
C,222 --->>> Sorted Elements - Provision for FIRST PASS
C,333 --->>> Sorted Elements - Provision for FIRST PASS
D,333 --->>> Sorted Elements - Provision for FIRST PASS
D,222 --->>> Sorted Elements - Provision for FIRST PASS
D,111 --->>> Sorted Elements - Provision for FIRST PASS
A,111 --->>> Sorted Elements - Provision for SECOND PASS
A,333 --->>> Sorted Elements - Provision for SECOND PASS
A,222 --->>> Sorted Elements - Provision for SECOND PASS
B,333 --->>> Sorted Elements - Provision for SECOND PASS
B,222 --->>> Sorted Elements - Provision for SECOND PASS
B,111 --->>> Sorted Elements - Provision for SECOND PASS
C,111 --->>> Sorted Elements - Provision for SECOND PASS
C,222 --->>> Sorted Elements - Provision for SECOND PASS
C,333 --->>> Sorted Elements - Provision for SECOND PASS
D,333 --->>> Sorted Elements - Provision for SECOND PASS
D,222 --->>> Sorted Elements - Provision for SECOND PASS
D,111 --->>> Sorted Elements - Provision for SECOND PASS

My results did not change on the SECOND PASS. Here is the code:

int main(){
   
    CSortRecord *precord = new CSortRecord();
   
    FILE *fsource;
    FILE *ftemp;
    char strFile[512] = SOURCE;
    char strTempFile[512] = TEMPFILE;
    char *tempStr[ELEMENTS];
//    char *tempSwap[ELEMENTS];
//    char *trgtStr[ELEMENTS];
    int chkKey = 0;
    int sampTest = 0;
    int n = 1;
    int k = 0;
    int i;
    int j;
    int m = 0;
//    int p;
       
    ftemp = fopen(strTempFile, "w");
    if((fsource = fopen(strFile, "r")) != NULL){
        cout << "File is valid" << endl << endl;
        while(!feof(fsource)){
            tempStr[k] = precord->Fill(fsource);
            if(strcmp(tempStr[k], "") != 0)
                k++;
        }
        cout << k << " --> Number of Elements" << endl << endl;
        ////DISPLAY UNSORTED ITEMS/////////
        for(i = 0; i < k; i++){
            cout << tempStr << " ---> Unsorted Elements" << endl;
        }

        cout << endl;

/////////////////ADDED SELECTION SORT FOR TESTING/////////
       ////// Selection Sort:: FIRST PASS/////
        for (j = 0; j < k; j++)
        {
            int minIndex = j;

            // Find the index of the Minimum element
            for (int q = j + 1; q < k; q++)
            {
               
                if(precord->CheckKey(tempStr[q], tempStr[minIndex]) == SWAPSTRINGS){
                    minIndex = q;
                }
       
            }
            // Swap
            if (minIndex > j)
            {
                precord->Swap(tempStr[j], tempStr[minIndex]);
            }
        }
        /////////ENDED Selection Sort FIRST PASS///////////

        ///TODO: IF FIRST FIELD IS EQUAL; CHECK SECOND FIELD//////
        /////////START Selection Sort SECOND PASS/////////////
        for(m = 0; m < k; m++){
            cout << tempStr[m] << " ---->>> Sorted Elements - Provision for FIRST PASS" << endl;

        }
        /////////ENDED Selection Sort SECOND PASS////////////
        for(int num = 0; num < k ; num++){
            cout << tempStr[num] << " ---->>> Sorted Elements - Provision for SECOND PASS" << endl;
        }

        cout << endl;

    } else {
        cout << "File is invalid" << endl;
    }

    delete precord;
    delete [] *tempStr;
    return 0;
}

Help please. I can't seem to find the problem in the code. Thank you...


Answer this question

Problem on Sorting Strings

  • Lange

    I don't understnad your problem. You don't need this two phases.

    Just think about each record as an entity. Compare each records against each other and use a standard sorting algorithm like bubblesort, quicksort, shellsort...

    To compare to records first compare part one, if they differ you now that the record must be inserted before or after. If the first part is euqal you check the second part and you now if its less equal or greater.

    You don't need this 2 compare phases!



  • magendo_man

    What is the meaning of the strings
    Part one is simply a string, part two is a number

    OK:

    Let us assume you used CStdioFile to the the file and you have two records
    CString r1, r2;

    Let us write a compare function:

    // returns 0 for equal, -1 if r1<r2, 1 for r1>r2;
    int CompareRecords(const CString &r1, const CString &r2)
    {
     // Find comma in records, (no error check!!!)
     int iPos1 = r1.Find(','),
         iPos2 = r2.Find(',');
       
     // Compare the first part, up to (not including) the comma position
     int iPosMin = min(iPos1,iPos2)-1;
     int iResult = strncmp(r1,r2,iPosMin);
     
     // If we have a differnece here we can stop the compare
     if (iResult!=0)
      return iResult;
      
     // The first part of the string is compared, but we are equal
     // the longer string is interpreted
     if (iPos1>iPos2)
      return 1;
     else if (iPos1<iPos2)
      return -1;
      
     // First part is completely equal. Get the number form part 2
    // just point behind the comma and get the numbers.
     int iNo1 = atoi(r1.GetString()+iPos1),
         iNo2 = atoi(r2.GetString()+iPos2);
        
     // Compare the numbers
     if (iNo1>iNo2)
      return 1;
     else if (iNo1<iNo2)
      return -1;
     else
      return 0;
    }
     



  • Jim in Topsfield

    but it is not sorted according to the letter and number. I'm thinking of splitting the string and comparing it individually. but how do I split the letter from the numbers I have this code that doesn't seem to work it give me a hexadecimal value. here is my code:

    m = 0;
            while(m < k)
            {
                CString str1;
                CString str2;
                strcpy(sTest1, tempStr[m]);
                strcpy(sTest2, tempStr[m + 1]);
                //str1 = sTest1;
                //str2 = sTest2;
                cout << sTest1 << endl;
                cout << sTest2 << endl;
                CQStringParser p1(sTest1, ','); //the parser I got from the net
                CQStringParser p2(sTest2, ',');
                //p1.GetField(1) gives me : 003426AC
              //p1.GetField(2) gives me : 00342704
                test1 = atoi(p1.GetField(2));
                test2 = atoi(p2.GetField(2));
                if(test1 > test2)
                {
                    strcpy(tempStr1[tempcount], tempStr[m]);
                    strcpy(tempStr1[tempcount + 1], tempStr[m + 1]);
                    //tempStr1[tempcount] = tempStr[m];
                    //tempStr1[tempcount + 1] = tempStr[m + 1];
                   
                }
                else
                {   
                    strcpy(tempStr1[tempcount], tempStr[m + 1]);
                    strcpy(tempStr1[tempcount + 1], tempStr[m]);
                    //tempStr1[tempcount] = tempStr[m + 1];
                    //tempStr1[tempcount + 1] = tempStr[m];
                }
                tempcount = tempcount + 2;
                m++;
            }

    it's kinda weird actually
    can you suggest other parsing or splitting techniques
    thanks

  • Problem on Sorting Strings