Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// Consider below class

class CAnimalData 
{
public:
	CAnimalData(int id, CString text)
	{
		m_id = id;
		m_text = text;
	}
	
	int		m_id;
	CString	m_text;


};

//lets create an array of class

typedef CArray <CAnimalData, CAnimalData&> RADARDATACONTAINER;

//create object of array
RADARDATACONTAINER  objContainer


//Now lets say we create 4 objects of class CAnimalData and keep storing all objects in CArray

CAnimalData data1 ( 0, "Simple")
objContainer.SetAtGrow(data1.m_id, dataBool);

CAnimalData data2 ( 1, "Cat")
objContainer.SetAtGrow(data2.m_id, dataBool);

CAnimalData data3 2, "Aargalis")
objContainer.SetAtGrow(data3.m_id, dataBool);

CAnimalData data4 ( 3, "Dog")
objContainer.SetAtGrow(data4.m_id, dataBool);

store all objects in RADARDATACONTAINER.

The class had two variables, please consider below values.

m_id   m_text
0	   Simple
1      Cat
2     Aargalis
3     Dog


Now I want to sort the CArray by m_text ( not by m_id), so below code should give me result in alphabetical order , how can I sort CArray?

CAnimalData dataStore;
iUBound = objContainer.GetUpperBound();

for(int i = 0; i <= iUBound; i++)

{
		dataStore = objContainer.GetAt(i);
	    Cout<< dataStore.m_id << "   "   << dataStore.m_text;
		
		
	    
}

I see below output:

0	   Simple
1      Cat
2     Aargalis
3     Dog


But I need below output: ( sorting based on text)

2     Aargalis
1     Cat
3     Dog
0	  Simple

Can anybody help me on this?


What I have tried:

tried but did not work, not sure if there is readymade funtion available in CArray class.
Will be great if anyone can help me here
Posted
Updated 14-Mar-18 6:09am
Comments
joyjhonson 14-Mar-18 10:25am    
Please note after filling values in Carray object , I want to sort based on m_text.

1 solution

You can't use RTL functions like qsort so you will have to write your own. For a very small set of data a bubble sort will work adequately. You will also have to write your own swap routine and those are usually fairly simple. Typically, a bubble sort is something like this :
C++
int count = collection.size();
for( int m = 0; m < count-1; ++m )
{
   for( int n = m + 1; n < count; ++n )
   {
       if( collection[m] > collection[n] )
       {
           collection.swap( m, n );
       }
   }
}
Here's an example of swapping :
C++
temp = a;
a = b;
b = temp;
In this case, a and b were the two items being checked in the bubble sort.

Your task will be to translate this pseudo code into real code. I won't do that for you because you will learn nothing from that. Hopefully, you can learn something in the process of writing real code for this algorithm. For example, the pseudo code shows a comparison of two items in the collection so how would you perform that comparison if you want to sort on the basis of the text? The answer is use to a comparison method of the CString class. I will let you figure that out too.

Remember, the debugger is your friend so if you run into troubles use it to help you figure out what is going on. I still use it daily so it is a good idea to become very familiar with it.

One tip: you can switch the order of the sort by changing the comparison from greater than to less than. You should try both to verify your implementation if you have the time. Best of luck.
 
Share this answer
 
v2
Comments
joyjhonson 16-Mar-18 7:38am    
thanks for input. Helpful, though I could not see swap method of CArray class.
Rick York 16-Mar-18 10:26am    
I don't know if there is one but that's something you should write yourself. It usually involves the copy constructor and assignment operators which are useful things to know about in C++.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900