Solved

STL sorting

Posted on 1998-02-08
1
294 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

Industry Leaders: 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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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 viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
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.

691 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