Click here to Skip to main content
Click here to Skip to main content
Go to top

Order MFC array with qsort()

, 6 Feb 2003
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Luca Basso Ricci
Software Developer (Senior)
Italy Italy
Got interested in C/C++ during high school,working as developer since 1998.
 
I started with C++/MFC for various client application.
 
From 2003,for five years,worked as web developer with Java/Hibernate/Spring for server side and JSP/CSS/Prototype on client side.
 
Along all 2009 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).
 
Currently developing a legacy data conversion software with Java/Spring heavly based on SQL/Hibernate.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralSorting of a Pointer Array Pinmemberpaula210225-Jul-03 3:45 
Eek! | :eek: Hallo,
It works very fine with CStrings etc.
then I tried it with sorting of dynamically allocated elements in an array
 
c_arraysort, CCXControl>(m_Objects, CCXControl::sort_left);
 
of the class:
 
class CCXControl
{
...
 
static int _cdecl sort_left(const CCXControl* pControl1, const CCXControl* pControl2)
{
CRect rect1 = (*pControl1)->GetRect();
CRect rect2 = (*pControl2)->GetRect();
return rect1.TopLeft().x < rect2.TopLeft().x ? -1 : 1;
}
...
}
 
I know that it shouldn'd work but have no solution.
please help.

 
Dirk

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140905.1 | Last Updated 7 Feb 2003
Article Copyright 2003 by Luca Basso Ricci
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid