Solved

C++ STL list.sort Function problem

Posted on 2013-11-11
3
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

615 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