Introduction
MFC CArray
doesn't implement an ordering function, but with a little trick it is possible to order a CArray
based-class (or a CStringArray
, CDWordArray
etc.) using the fast qsort()
function included in stdlib.
This template class allows to order an array of every type by declaring the type of array and the type of every element contained into. For example, to sort a CStringArray
using the default ascending function:
CStringArray array;
c_arraysort<CStringArray,CString>(array,sort_asc);
in the same way is possible to order a custom struct
(or class
):
class myClass
{
public:
myClass() {}
CString str;
long n;
bool operator<(const myClass& elem2) const
{ return str < elem2.str;}
bool operator>(const myClass& elem2) const
{ return str > elem2.str;}
static int _cdecl sort_by_str(const myClass* elem1,const myClass* elem2)
{ return elem1->str < elem2->str ? -1 : 1; }
static int _cdecl sort_by_n(const myClass* elem1,const myClass* elem2)
{ return elem1->n < elem2->n ? -1 : 1; }
};
CArray<myClass,myClass> myArray;
c_arraysort<CArray<myClass,myClass>,MyClass>(myArray,sort_asc);
c_arraysort<CArray<myClass,myClass>,MyClass>(myArray,sort_desc);
c_arraysort<CArray<myClass,myClass>,MyClass>(myArray,myClass::sort_by_str);
c_arraysort<CArray<myClass,myClass>,MyClass>(myArray,myClass::sort_by_n);
Implementation
Class is quite simple:
- two
typedef
are used only to prevent long cast between generic compare function prototype (used by qsort
) and the custom one (used by c_arraysort
),
- function
CArray::GetData()
is used to get a pointer to the first array element passed as first parameter in qsort()
,
Ctor
calls sorting function, in this mode using the class is like calling a function.
I had interest in C/C++ during high school, I'm working as developer since 1998.
2016-Now : I'm working on a basic debugger for DSL using JDI.
2014-2016: Small project focused on XBRL files production.
Built a stand-alone engine for XBRL.
Engine is capable to parse XBRL taxonomies and read/write XBRL instance files.
2010-2014: I have worked on a data conversion program from legacy system software heavly based on SQL/Hibernate/Spring written in Java.
Advanced use of Spring framework and its built-in aspect programming.
This program was thought and written from scratch only by me,so I was involved not only as developer but also as architect.
2009-2010 : Worked on a CASE suite used to create tax programs based on data models and DSL.
I had a deep experience about application engineering and embedded DSL creation with ANTLR (as part of a CASE suite used to create tax programs based on data models).
2004-2009 : Worked as web developer with Java/Hibernate/Spring for server side and JSP/CSS/Prototype on client side.
1998-2004 : I started with C++/MFC for various client application.
MS/Access and SQLServer for data storage and COM for modules inter-operability.