Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:48:06+00:00 2026-05-31T01:48:06+00:00

I get some strange behavior when sorting a c++ vector with at custom comparator

  • 0

I get some strange behavior when sorting a c++ vector with at custom comparator class.

I want to sort a array of indexes based on some values in an other array. But as far as I can see the sort function just reverses my indexes. I’ve done some logging of the compare function, at it seems to be working just fine.

Is there anyone who can spot what I’m doing wrong?

My code:

template<class T>
class Comparator {
   vector<T> & data;
public:
  bool operator()(int a, int b) {
    return data.at(a) < data.at(b) ? -1 : (data.at(a) > data.at(b) ? 1 : 0);
  }

  Comparator(vector<T> & data) : data(data) {}
};

void sortTest2() {
  //SETUP
  int n = 5;
  vector<int> indexes(n);
  newIdAr(indexes, n); //init to {0,1,2,3,4}
  vector<double> data(n);
  randomData(data, n); //Init to radom data
  Comparator<double> comparator(data);

  //TEST
  print(indexes, data, n); // Prints [0.00125126, 0.563585, 0.193304, 0.808741]
  sort(indexes.begin(), indexes.end(),comparator);
  print(indexes, data, n); // Prints [0.808741, 0.193304, 0.563585, 0.00125126]
  sort(indexes.begin(), indexes.end(),comparator);
  print(indexes, data, n); // Prints [0.00125126, 0.563585, 0.193304, 0.808741]
  cout << "Shuffle" << endl;
  random_shuffle(indexes.begin(), indexes.end());
  print(indexes, data, n);
  sort(indexes.begin(), indexes.end(), comparator);
  print(indexes, data, n);
}

My output:

[0.00125126, 0.563585, 0.193304, 0.808741, 0.585009]
[0.585009, 0.808741, 0.193304, 0.563585, 0.00125126]
[0.00125126, 0.563585, 0.193304, 0.808741, 0.585009]
Shuffle
[0.193304, 0.00125126, 0.585009, 0.563585, 0.808741]
[0.808741, 0.563585, 0.585009, 0.00125126, 0.193304]

Code for the auxiliary functions:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print(vector<int> & sort, vector<double> & data, int N) {
  cout << "[";
  for (int i = 0; i < N - 1; ++i) {
    cout << data[sort[i]] << ", ";
  }
  cout << data[sort[N - 1]] << "]" << endl;
}

void newIdAr(vector<int> & ar, int N) {
  for (int i = 0; i < N; ++i) {
    ar[i] = i;
  }
}

void randomData(vector<double> & data, int n) {
  for (int i = 0; i < n; ++i) {
    data[i] = ((double) rand()) / RAND_MAX;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:48:08+00:00Added an answer on May 31, 2026 at 1:48 am

    Comparators should return true if the first argument is lower than the second one, or false otherwise. Returning 0, -1 and 1 is invalid in your code.

    You could realize this by analizing the comparator’s operator() signature:

    bool operator()(int a, int b)
    

    I believe your implementation should be:

    bool operator()(int a, int b) {
        return data.at(a) < data.at(b);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get some strange behavior when inserting values into a map: I always insert
I've come across some strange behavior trying to get files that start with a
I see some strange behavior on the maximum heap size I get on Sun's
To get some smooth graphics, I want to draw oversampled by factor 2 and
For some reason I get some warnings about non dll-interface class when building with
I would appreciate help debugging some strange behavior by a multiset container. Occasionally, the
I'm trying to remove the image border in ie, but I get some strange
I encountered some strange behavior which hints that I do not understand some basic
I'm getting some strange behavior... when I iterate over the dummyText List in the
I am experiencing some strange behavior with OSQL, and would appreciate any help. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.