Solved

STL sorting

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

789 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