Solved

STL sorting

Posted on 1998-02-08
1
293 Views
Last Modified: 2013-12-14
I am using the Standard Template Library in a program that involves sorting linked lists.  I have an object defined that contains two variables, int ID and char TYPE.  I also have defined the operator< and operator == which involves the ID variable.  So an STL sort would sort the dequeue by the ID number.  I also want to sort by the TYPE.  For example, If I am sorting by ID number, and two identical number come up, I want to be able to break the tie by choosing the object with the highest TYPE priority.  I would also like to be able to sort by TYPE.  Is there any way to use the STL to sort a dequeue of object by more than one object variable?  
            Thank you
            Chris Blue
0
Comment
Question by:nexguy
[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
1 Comment
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1181541
The standard allows you to pass a comparator to list::sort() as a parameter. However, most compilers do not yet support template member functions, and therefore they do not support this function.

The global sort, however, does not require this feature. It works on a deque, vector and built-in arrays. Here is an example:

#include <vector>
#include <algorithm>

struct MyStruct {
    int i;
    char c;
};

bool Compare_I(const MyStruct s1, const MyStruct s2)
{return (s1.i > s2.i);}

bool Compare_C(const MyStruct s1, const MyStruct s2)
{return (s1.c > s2.c);}

bool Compare_Both(const MyStruct s1, const MyStruct s2)
{
    if (s1.i == s2.i) return (s1.c > s2.c);
    else return (s1.i > s2.i);
}

int main ()
{
    using std::vector;
    using std::sort;

    vector<MyStruct> v;

    // put stuff into v

    sort(v.begin(), v.end(), Compare_I); // sort by first field
    sort(v.begin(), v.end(), Compare_C); // sort by second field
    sort(v.begin(), v.end(), Compare_Both); // sort by both

    return 0;
}


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

734 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