Solved

C++ STL list.sort Function problem

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ standard library based binary archive format 6 98
sorting efficency of sorting algorithm 30 113
Which Linux flavors will this run on? 6 88
designing in object programming 12 78
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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