Questions tagged [custom-compare]

26 questions
10
votes
2 answers

Why can not we use `std::multiset` with custom compare lambda as the value of a `std::map`?

This is a follow-up question of asked How to provide custom comparator for `std::multiset` without overloading `operator()`, `std::less`, `std::greater`? and I have tried to solve by the following manner. Basic One can provide custom compare lambda…
JeJo
  • 30,635
  • 6
  • 49
  • 88
9
votes
2 answers

LinQ distinct with custom comparer leaves duplicates

I've got the following classes: public class SupplierCategory : IEquatable { public string Name { get; set; } public string Parent { get; set; } #region IEquatable Members public bool…
NDM
  • 6,731
  • 3
  • 39
  • 52
6
votes
1 answer

how to sort the child nodes of treeView

I want to sort a given TreeView child nodes in alphabetical order. Suppose my tree View is like this: firstNode1 secondNode1 thirdNode1 thirdNode2 thirdNode3 ... firstNode2 secondNode1 thirdNode1 thirdNode2 thirdNode3 ... I want to sort the…
theburningfire
  • 481
  • 1
  • 4
  • 20
5
votes
3 answers

How to use priority_queue with a non-static compare method of class instance?

Suppose I have a simple class like this: class Test { public: Test(int reference) { m_reference = reference; } void feed(int x) { m_data.push_back(x); } int get() { return m_data.front(); } private: int m_reference; std::vector
Delgan
  • 18,571
  • 11
  • 90
  • 141
4
votes
1 answer

How to sort a Hashmap on Key based on Custom Comparator

I am receiving a Hashmap for a common process that I need to sort on the Key using a custom comparator. Below is what I have tried, but it does not seem to work - it is not sorting the keys. The keys of the map are of the form long-string-short…
adbdkb
  • 1,897
  • 6
  • 37
  • 66
4
votes
3 answers

How to provide custom comparator for `std::multiset` without overloading `operator()`, `std::less`, `std::greater`?

I want a custom comparator for the following code. However, I am not allowed to overload operator(), std::less, std::greater. I tried to achieve this using lambda but gcc won't allow me to use auto as a non-static member. Any other way to make…
3
votes
2 answers

Custom compare function for std::multimap when keys are equal

I would like to code a custom comparator for a std::multimap. What I would like to do is to compare the keys, in case they are equal, then compare the values. I'm trying to do it by overloading the operator() in a struct and passing the function…
Jules
  • 134
  • 1
  • 10
2
votes
2 answers

std::set of MyElement with MyElement::SomeMethod as custom comparator

I have a simple MyElement class, and I would like to use a bool MyElement::SomeMethod(...) {...} as the custom comparator for a std::set of MyElement items. I have made my research, and I am already aware of some alternative solutions, which I list…
vaeVictis
  • 484
  • 1
  • 3
  • 13
2
votes
1 answer

How to sort w.r.t. certain parameter using C++ custom compare function?

I am trying to sort points on a plane by their polar angle w.r.t. the point O. The simplified version of the code looks like this: bool comparePolar(point A, point B, point O){ //if point B lies to the left of the edge OA, return false …
2
votes
3 answers

How to sort std::set according to the second element?

Given n points in a two-dimensional space, sort all the points in ascending order. (x1,y1) > (x2,y2) if and only if (x1>x2) or (x1==x2 && y1
user11376749
2
votes
3 answers

Should a custom comparer for strings allow for null values

I'm looking at someone elses code for a custom comparer that compares strings. I'm noticing that it will fall over if at least one of the string parameters is null. The compare returns -1, 0 or 1 based on the result of the comparison. Should code…
Coder 2
  • 4,761
  • 12
  • 40
  • 43
1
vote
1 answer

Using same function for sorting a vector and custom comparator in set

This might sound like a stupid problem but I wondered for a long time is there a better way that this: struct X { int a; int b; }; bool sortComp(const X first, const X second) { if (first.a!=second.a) return (first.a
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
0 answers

How to use sorted using key=lambda instead of cmp_to_key?

Given an array A of non-negative integers, arrange them such that they form the largest number. example : A = [3, 30, 34, 5, 9] output : "9534330" I am able to do this using custom compare function, is it possible to achieve the same using…
1
vote
3 answers

Why does this simple custom-comparator of tuples crash even with strict-weak ordering?

This simple custom-comparator for type tuple crashes for the example test below. I checked with the cout statements in the cmp comparator that each call to cmp gets a return value, so it's not like the values in the tuples t1 and t2…
Joe Black
  • 625
  • 6
  • 19
1
vote
2 answers

Writing a proper implementation of compareTo

private static class CharacterIndex implements Comparable { private final char c; private final int index; public CharacterIndex(char c, int index) { this.c = c; this.index = index; } } Now I want…
Lavish Kothari
  • 2,211
  • 21
  • 29
1
2