Click here to Skip to main content
15,878,959 members
Articles / Desktop Programming / MFC
Article

Order MFC array with qsort()

Rate me:
Please Sign up or sign in to vote.
4.54/5 (18 votes)
6 Feb 2003CPOL 59.1K   830   23   1
How to order an MFC array using standard function qsort().

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;
//  ... Add some strings to 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;

  //With this two operator will be possible to
  //order using default c_arraysort sorting function
  bool operator<(const myClass& elem2) const
  { return str < elem2.str;}
  bool operator>(const myClass& elem2) const
  { return str > elem2.str;}

  // Custom ascending ordering function (order by str or by n)
  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;

// Sort using operator <
c_arraysort<CArray<myClass,myClass>,MyClass>(myArray,sort_asc);
// Sort using operator >
c_arraysort<CArray<myClass,myClass>,MyClass>(myArray,sort_desc);

// Sort using custom function
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.

License

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


Written By
Software Developer (Senior)
Italy Italy
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.

Comments and Discussions

 
GeneralSorting of a Pointer Array Pin
paula210225-Jul-03 3:45
paula210225-Jul-03 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.