Bubble sort in CObList [modified]
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Here is a useful article on CodeProject to get you started: Using the std::sort() Method[^].
Just say 'NO' to evaluated arguments for diadic functions! Ash
The article sorts a STL collection class, but this just complement my answer ;)
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Well , I must change much code to use std::sort ( plus , I never used before ) , it is the only way ? I mean , how can I switch position between two elements of an objects array ?
-
Well , I must change much code to use std::sort ( plus , I never used before ) , it is the only way ? I mean , how can I switch position between two elements of an objects array ?
mesajflaviu wrote:
plus , I never used before
This is just a bad excuse ;P . The STL collection classes are much more powerful than the MFC collection classes, I strongly suggest you start using them instead of the MFC collections, you won't regret it (it takes a bit of time to get started with it but once you know how to use them, you will gain a lot of time).
mesajflaviu wrote:
I mean , how can I switch position between two elements of an objects array ?
You can still remove an element and insert it back at the correct location but this is ugly and prone to errors. I'm not aware of another alternative (but I didn't use the MFC collections for long).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
mesajflaviu wrote:
plus , I never used before
This is just a bad excuse ;P . The STL collection classes are much more powerful than the MFC collection classes, I strongly suggest you start using them instead of the MFC collections, you won't regret it (it takes a bit of time to get started with it but once you know how to use them, you will gain a lot of time).
mesajflaviu wrote:
I mean , how can I switch position between two elements of an objects array ?
You can still remove an element and insert it back at the correct location but this is ugly and prone to errors. I'm not aware of another alternative (but I didn't use the MFC collections for long).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++I think that I try in both way .. thanks !
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
// switch pObj1 and pObj2 between them ... but how ? Try it :) :
void SortDrawObjects(CDrawObjList& cList)
{
for (int i = 0; i < cList.GetCount(); i++) {
POSITION pos = cList.GetHeadPosition();
while (pos) {
POSITION posFirst = pos;
CDrawObj* pcFirst = cList.GetNext(pos);
if (pos) {
POSITION posSecond = pos;
CDrawObj* pcSecond = cList.GetNext(pos);
if (pcFirst->m_position.left > pcSecond->m_position.left) {
cList.SetAt(posFirst, pcSecond);
cList.SetAt(posSecond, pcFirst);
}
pos = posSecond;
}
}
}
}They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
-
Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Well, you could use the stl algorithms work with the MFC collection classes too. That's the unprecedented beauty of STL. :)
It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
An algorithm after all is just a modus operandi. As long as a collection class, irrespective of the library that is part of, provides mechanism to insert and remove elements at any location, all the algorithms can be implemented. The performance may be not as expected due to the implementation of the classes themselves. What I am trying to say is, if you understand the algorithm and read the documentation for the collection class, it must not be difficult at all.
...byte till it megahertz... my donation to web rubbish
-
// switch pObj1 and pObj2 between them ... but how ? Try it :) :
void SortDrawObjects(CDrawObjList& cList)
{
for (int i = 0; i < cList.GetCount(); i++) {
POSITION pos = cList.GetHeadPosition();
while (pos) {
POSITION posFirst = pos;
CDrawObj* pcFirst = cList.GetNext(pos);
if (pos) {
POSITION posSecond = pos;
CDrawObj* pcSecond = cList.GetNext(pos);
if (pcFirst->m_position.left > pcSecond->m_position.left) {
cList.SetAt(posFirst, pcSecond);
cList.SetAt(posSecond, pcFirst);
}
pos = posSecond;
}
}
}
}They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
Well , it's work great ! Thank you very much !
-
Well, you could use the stl algorithms work with the MFC collection classes too. That's the unprecedented beauty of STL. :)
It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.
If I mix MFC with STL , I wouldn't keep consistency , or not ?
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
-
Try google: "sort coblist" comes up with http://www.codeguru.com/cpp/cpp/cpp_mfc/collections/article.php/c757/[^] and a lot of recommendations to drop MFC :)
Useful code !
-
Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++It's been a while (and I don't have my book with me) but iirc, the std::algorithm (and I think sort is in that) didn't require the structure to be a std::list. (I seem to recall standard arrays being able to be sorted with std as well.)
-
It can be posible to (bubble) sort an ObList after some criteria ? Here I have :
typedef CTypedPtrList<> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
// which sizeRect , and ptOrigin will be aleatory
ObjList.AddTail(pObj)
}ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition(); while(pos) { CDrawObj\* pObj1 = ObjList.GetNext(pos); if(pos == NULL)break; CDrawObj\* pObj2 = ObjList.GetNext(pos); if(pObj1->m\_position.left > pObj2->m\_position.left) // switch pObj1 and pObj2 between them ... but how ? }
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
If you are wedded to MFC, at least use a CObArray instead of a CObList. The algorithim will be much cleaner and faster with a smaller memory footprint. For instance ...
typedef CTypedPtrArray<> CDrawObjArray;
then create the array ...
CDrawObjArray ObjArray;
const unsigned int nSize = 5;
ObjArray.SetSize(nSize);
fill the array ...
for(int i = 0; i < nSize; i++)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));ObjArray\[i\] = pObj;
}
and finally sort the array (using the most inefficient sorting algorithim known to man).
for(int i = 0; i < nSize; i++)
{
for(int j = i; j < nSize; j++)
{
unsigned int nPrevious = ObjArray[j-1]->m_position.left;
unsigned int nCurrent = ObjArray[j]->m_position.left;if(nPrevious > nCurrent) { CDrawObj\* pObj = ObjArray\[j-1\]; ObjArray\[j-1\] = ObjArray\[j\]; ObjArray\[j\] = pObj ; } }
}
-
If you are wedded to MFC, at least use a CObArray instead of a CObList. The algorithim will be much cleaner and faster with a smaller memory footprint. For instance ...
typedef CTypedPtrArray<> CDrawObjArray;
then create the array ...
CDrawObjArray ObjArray;
const unsigned int nSize = 5;
ObjArray.SetSize(nSize);
fill the array ...
for(int i = 0; i < nSize; i++)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));ObjArray\[i\] = pObj;
}
and finally sort the array (using the most inefficient sorting algorithim known to man).
for(int i = 0; i < nSize; i++)
{
for(int j = i; j < nSize; j++)
{
unsigned int nPrevious = ObjArray[j-1]->m_position.left;
unsigned int nCurrent = ObjArray[j]->m_position.left;if(nPrevious > nCurrent) { CDrawObj\* pObj = ObjArray\[j-1\]; ObjArray\[j-1\] = ObjArray\[j\]; ObjArray\[j\] = pObj ; } }
}
Michael Waters wrote:
...(using the most inefficient sorting algorithim known to man).
I can think of one worse.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Michael Waters wrote:
...(using the most inefficient sorting algorithim known to man).
I can think of one worse.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
Guys , you're overcome me with solutions !!! I want to kindly thank you ! I was learn something here .And after all , seems to must begin use STL ... I will googling and see what I find ... You are very kind !