Solved

C++ STL list.sort Function problem

Posted on 2013-11-11
3
355 Views
Last Modified: 2013-11-11
I am trying to sort an STL list using the list.sort function with a compare function argument.
The list is a list of pointers to a structure type and I wish to sort the list on the basis of the value of one of the structure members (called order).
The code compiles and runs but the sort function just reverses the existing list order regardless of the values of the structure member.
I am using borland C++ Builder 5.

The code is attached.

The displayed results are as follows:
10
11
40
30
2
15
sorted
15
2
30
40
11
10
re-sorted
10
11
40
30
2
15
Unit1.h
Unit1.cpp
0
Comment
Question by:alcindor
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Your comparator function is

int __fastcall Compare(void *item1,void *item2)
{   // used for ordering the list according to the order member of the structure
    Mystructure *ms1 = (Mystructure *)item1;
    Mystructure *ms2 = (Mystructure *)item2;

    return ms1->order - ms2->order;
}

Open in new window


but it should be a binary predicate returning 'true' or 'false'. If you want the sort order to be ascending to 'order', you shouzld make that

bool __fastcall Compare(void *item1,void *item2)
{   // used for ordering the list according to the order member of the structure
    Mystructure *ms1 = (Mystructure *)item1;
    Mystructure *ms2 = (Mystructure *)item2;

    return ms1->order < ms2->order;
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:alcindor
Comment Utility
Excellent !
Thanks for that.
I was mis-led by the help on TList::sort which uses a compare function that returns an int.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, I thought so, since your comparator reminded me of something similar - yet seeing the 'int' return value rang an alarm bell ;o)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now