Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ STL list.sort Function problem

Posted on 2013-11-11
3
Medium Priority
?
410 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 2000 total points
ID: 39639688
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
ID: 39639727
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
ID: 39639773
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

926 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