Solved

C++ STL list.sort Function problem

Posted on 2013-11-11
3
367 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 89
Find Visual Studio Tools 2 113
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 194
designing in object programming 12 94
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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