We help IT Professionals succeed at work.

Efficient method to sort a CPtrarray?

XPUSR asked
Medium Priority
Last Modified: 2013-11-20
What is the Efficient method to sort a CPtrarray in c++?
Watch Question

Author of the Year 2009
Use qsort... as described here:
    How to use the quick-sort function to sort MFC CArray-derived classes on the MFC and Visual C++ run-time library

You will of course need to sort not on the *value* of the pointer itself, but on the value of the data to which each item points.  You write that logic into your Compare() function.

-- Dan

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

It depends on what you mean by efficient and a good deal on the type of data. For example, if most of the values are the same except just a few or most of them are already sorted then, believe it or not, bubblesort would be your best bet, with the normal tuning techniques.

Tell us a bit more about the data. Whats in it? How much does it vary? Can you add them one-at-a-time or do you get them in one big block? Etc ...

AndyAinscowFreelance programmer / Consultant

What is efficient.
The bubble sort is very easy to code but takes a lot of time to sort compared to more advanced techniques.
However, if the array has, say, 8 pointers would the end user really notice it takes 0.001 seconds longer to execute?

(Bubble sort is just go through the array and swap adjacent items if they are not in order.  You keep doing that until no swaps are made.)


Thanks for the suggestions. How about conveting it to a vector and using the stl sort. How does this compare? The data in an array of stock performances.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.