Questions tagged [comparison]

Questions about data comparison and efficient ways to accomplish it. Please avoid using this tag for generic (meta) comparison of two issues or concepts.

Questions about data comparison and efficient ways to accomplish it, e.g.: "Which is the fastest algorithm to compare two recordsets and decide which one has more palindromes?", or "What is the fastest way to compare two strings and say if they have more than Y characters in common?"

There are generally six basic "comparison operators": less than, <; less than or equal to, <=; equal to, ==; not equal to, !=; greater than or equal to, >=; and greater than, >.

8118 questions
174
votes
6 answers

Why `null >= 0 && null <= 0` but not `null == 0`?

I had to write a routine that increments the value of a variable by 1 if its type is number and assigns 0 to the variable if not, where the variable is initially null or undefined. The first implementation was v >= 0 ? v += 1 : v = 0 because I…
Chungmin Lee
  • 2,320
  • 2
  • 18
  • 19
172
votes
11 answers

Python - doctest vs. unittest

I'm trying to get started with unit testing in Python and I was wondering if someone could explain the advantages and disadvantages of doctest and unittest. What conditions would you use each for?
Sean
  • 5,244
  • 6
  • 28
  • 27
163
votes
5 answers

Difference between "!==" and "==!"

Yesterday I stumbled over this when I modified PHP code written by someone else. I was baffled that a simple comparison (if ($var ==! " ")) didn't work as expected. After some testing I realized that whoever wrote that code used ==! instead of !==…
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
160
votes
5 answers

The 3 different equals

What is the difference between =, ==, and ===? I think using one equal sign is to declare a variable while two equal signs are for a comparison condition and lastly three equal signs are for comparing values of declared variables.
Strawberry
  • 66,024
  • 56
  • 149
  • 197
158
votes
9 answers

How to compare two JSON objects with the same elements in a different order equal?

How can I test whether two JSON objects are equal in python, disregarding the order of lists? For example ... JSON document a: { "errors": [ {"error": "invalid", "field": "email"}, {"error": "required", "field": "name"} ], …
user1635536
156
votes
8 answers

Trouble comparing time with RSpec

I am using Ruby on Rails 4 and the rspec-rails gem 2.14. For a my object I would like to compare the current time with the updated_at object attribute after a controller action run, but I am in trouble since the spec does not pass. That is, given…
Backo
  • 18,291
  • 27
  • 103
  • 170
152
votes
4 answers

Why C# fails to compare two object types with each other but VB doesn't?

I have two objects in C# and don't know if it's Boolean or any other type. However when I try to compare those C# fails to give the right answer. I have tried the same code with VB.NET and that did it ! Can anyone tell me how to fix this if there is…
Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86
151
votes
3 answers

How is __eq__ handled in Python and in what order?

Since Python does not provide left/right versions of its comparison operators, how does it decide which function to call? class A(object): def __eq__(self, other): print "A __eq__ called" return self.value == other class…
PyProg
  • 1,525
  • 2
  • 10
  • 4
137
votes
11 answers

Compare Strings Javascript Return %of Likely

I am looking for a JavaScript function that can compare two strings and return the likeliness that they are alike. I have looked at soundex but that's not really great for multi-word strings or non-names. I am looking for a function like: …
Brad Ruderman
  • 2,053
  • 2
  • 15
  • 22
134
votes
3 answers

C# vs Java generics

I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view?
johnc
  • 39,385
  • 37
  • 101
  • 139
129
votes
5 answers

JQuery string contains check

I need to check whether a string contains another string or not? var str1 = "ABCDEFGHIJKLMNOP"; var str2 = "DEFG"; Which function do I use to find out if str1 contains str2?
diggersworld
  • 12,770
  • 24
  • 84
  • 119
127
votes
4 answers

Should __ne__ be implemented as the negation of __eq__?

I have a class where I want to override the __eq__ method. It seems to make sense that I should override the __ne__ method as well. Should I implement __ne__ as the negation of __eq__ as such or is it a bad idea? class A: def __init__(self,…
Falmarri
  • 47,727
  • 41
  • 151
  • 191
126
votes
16 answers

What is "missing" in the Visual Studio 2008 Express Editions?

What is "missing" in the Visual Studio 2008 Express Editions? In particular, what functionality is not available? what restrictions are there on its use?
benefactual
  • 7,079
  • 5
  • 23
  • 16
124
votes
12 answers

How should I do floating point comparison?

I'm currently writing some code where I have something along the lines of: double a = SomeCalculation1(); double b = SomeCalculation2(); if (a < b) DoSomething2(); else if (a > b) DoSomething3(); And then in other places I may need to do…
Mike Bailey
  • 12,479
  • 14
  • 66
  • 123
124
votes
10 answers

How to compare enum with associated values by ignoring its associated value in Swift?

After reading How to test equality of Swift enums with associated values, I implemented the following enum: enum CardRank { case Number(Int) case Jack case Queen case King case Ace } func ==(a: CardRank, b: CardRank) -> Bool { …
Senseful
  • 86,719
  • 67
  • 308
  • 465