alphacentauri
alphacentauri

Reputation: 1020

Sorting a Vector of Vectors

I have a vector of vector containing elements of type long as follows:

vector< vector<long> > Data(N,vector<long>(M));

I have to sort these vectors based on their values i.e. For Two vectors

Data[i] & Data[j]
if for some k Data[i][k]< Data[j][k]
and Data[i][t]==Data[j][t] for all 0<=t<=(k-1),
then Data[i] should come before Data[j] in the final vector

Not for the above task I wrote the following code:

sort(Data.begin(),Data.end(),myfunc);

where

bool myfunc(vector<long> vec1,vector<long> vec2){
     int i=0;
     while(i<vec1.size()){
         if(vec1[i]<vec2[i]){
             return false;
         }
         else if(vec1[i]>vec2[i]){
             return true;
         }
         i++;
     }
     return false;
}

However, I am not getting the desired output. In fact the input and output vectors are the same. Where did I go wrong?? Am I missing something??

Upvotes: 3

Views: 5335

Answers (2)

Abhishek Bansal
Abhishek Bansal

Reputation: 12705

I tried implementing your code as is. It appears to sort the vectors in descending order. Try toggling the trues and falses from your myfunc function.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool myfunc(vector<long> vec1,vector<long> vec2){
     int i=0;
     while(i<vec1.size()){
         if(vec1[i]<vec2[i]){
             return false;
         }
         else if(vec1[i]>vec2[i]){
             return true;
         }
         i++;
     }
     return false;
}

int main()
{
    int N = 5, M = 5;

    vector< vector<long> > Data(N,vector<long>(M));
    for ( int i = 0; i < Data.size(); i++ ) {
        for ( int j = 0; j < Data[i].size(); j++ )
            Data[i][j] = 5-i;

    }

    sort( Data.begin(), Data.end(), myfunc );
    for ( int i = 0; i < Data.size(); i++ ) {
        for ( int j = 0; j < Data[i].size(); j++ )
            cout << Data[i][j] << " " ;
        cout << endl;

    }

    return 0;
}

Output:

5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1

After toggling the trues and falses, the following code sorts the vectors in correct order.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool myfunc(const vector<long> &vec1, const vector<long> &vec2){
     int i=0;
     while(i<vec1.size()){
         if(vec1[i]<vec2[i]){
             return true;
         }
         else if(vec1[i]>vec2[i]){
             return false;
         }
         i++;
     }
     return true;
}

int main()
{
    int N = 5, M = 5;

    vector< vector<long> > Data(N,vector<long>(M));
    for ( int i = 0; i < Data.size(); i++ ) {
        for ( int j = 0; j < Data[i].size(); j++ )
            Data[i][j] = 5-i;

    }

    sort( Data.begin(), Data.end(), myfunc );
    for ( int i = 0; i < Data.size(); i++ ) {
        for ( int j = 0; j < Data[i].size(); j++ )
            cout << Data[i][j] << " " ;
        cout << endl;

    }

    return 0;
}

Output

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

Upvotes: 1

Paul Draper
Paul Draper

Reputation: 83353

You have a couple mistakes, but not all are evident to you (yet).

bool myfunc(const vector<long>& vec1, const vector<long>& vec2){
    for(size_t i = 0; i < vec1.size() && i < vec2.size(); i++){
         if(vec1[i] > vec2[i]){
             return false;
         } else if(vec1[i] < vec2[i]){
             return true;
         }
    }
    return false;
}

I took the liberty of using a for loop and size_t, which are better practices here.

Upvotes: 2

Related Questions