Click here to Skip to main content
12,547,922 members (47,407 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked

Order MFC array with qsort()

, 6 Feb 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
How to order an MFC array using standard function qsort().


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

in the same way is possible to order a custom struct (or class):

class myClass
  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 <
// Sort using operator >

// Sort using custom function


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.


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


About the Author

Luca Basso Ricci
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.

You may also be interested in...

Comments and Discussions

GeneralSorting of a Pointer Array Pin
paula210225-Jul-03 3:45
memberpaula210225-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 7 Feb 2003
Article Copyright 2003 by Luca Basso Ricci
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid