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, >.

7504 questions
445
votes
22 answers

Comparing date part only without comparing time in JavaScript

What is wrong with the code below? Maybe it would be simpler to just compare date and not time. I am not sure how to do this either, and I searched, but I couldn't find my exact problem. BTW, when I display the two dates in an alert, they show as…
moleculezz
  • 6,643
  • 4
  • 23
  • 24
418
votes
10 answers

Image comparison - fast algorithm

I'm looking to create a base table of images and then compare any new images against that to determine if the new image is an exact (or close) duplicate of the base. For example: if you want to reduce storage of the same image 100's of times, you…
meade
  • 21,435
  • 12
  • 29
  • 36
408
votes
11 answers

How to compare dates in Java?

How do I compare dates in between in Java? Example: date1 is 22-02-2010 date2 is 07-04-2010 today date3 is 25-12-2010 date3 is always greater than date1 and date2 is always today. How do I verify if today's date is in between date1 and date 3?
ant
  • 21,609
  • 35
  • 125
  • 176
377
votes
5 answers

Jackson Vs. Gson

After searching through some existing libraries for JSON, I have finally ended up with these two: Jackson Google GSon I am a bit partial towards GSON, but word on the net is that GSon suffers from a certain celestial performance issue (as of Sept…
Suraj Chandran
  • 23,444
  • 11
  • 58
  • 92
364
votes
8 answers

Comparing two strings, ignoring case in C#

Which of the following two is more efficient? (Or maybe is there a third option that's better still?) string val = "AStringValue"; if (val.Equals("astringvalue", StringComparison.InvariantCultureIgnoreCase)) OR if (val.ToLowerCase() ==…
pwmusic
  • 3,869
  • 4
  • 21
  • 14
324
votes
9 answers

How to check if a variable is not null?

I know that below are the two ways in JavaScript to check whether a variable is not null, but I’m confused which is the best practice to use. Should I do: if (myVar) {...} or if (myVar !== null) {...}
nickb
  • 8,430
  • 11
  • 34
  • 46
297
votes
8 answers

Check if two unordered lists are equal

I'm looking for an easy (and quick) way to determine if two unordered lists contain the same elements: For example: ['one', 'two', 'three'] == ['one', 'two', 'three'] : true ['one', 'two', 'three'] == ['one', 'three', 'two'] : true ['one', 'two',…
Paul
  • 7,305
  • 10
  • 45
  • 71
294
votes
11 answers

What is the rationale for all comparisons returning false for IEEE754 NaN values?

Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN returns false, contrary to the behaviour of all other values. I suppose this…
starblue
  • 51,675
  • 14
  • 88
  • 146
291
votes
14 answers

What's the most efficient way to test two integer ranges for overlap?

Given two inclusive integer ranges [x1:x2] and [y1:y2], where x1 ≤ x2 and y1 ≤ y2, what is the most efficient way to test whether there is any overlap of the two ranges? A simple implementation is as follows: bool testOverlap(int x1, int x2, int y1,…
WilliamKF
  • 36,283
  • 61
  • 170
  • 271
276
votes
11 answers

Differences in string compare methods in C#

Comparing string in C# is pretty simple. In fact there are several ways to do it. I have listed some in the block below. What I am curious about are the differences between them and when one should be used over the others? Should one be avoided…
Craig
  • 10,504
  • 13
  • 40
  • 62
274
votes
26 answers

Comparing two dictionaries and checking how many (key, value) pairs are equal

I have two dictionaries, but for simplification, I will take these two: >>> x = dict(a=1, b=2) >>> y = dict(a=2, b=2) Now, I want to compare whether each key, value pair in x has the same corresponding value in y. So I wrote this: >>> for x_values,…
user225312
  • 108,033
  • 64
  • 161
  • 179
269
votes
10 answers

Switch statement for greater-than/less-than

so I want to use a switch statement like this: switch (scrollLeft) { case (<1000): //do stuff break; case (>1000 && <2000): //do stuff break; } Now I know that either of those statements (<1000) or (>1000 && <2000) won't work (for…
switz
  • 21,124
  • 20
  • 72
  • 100
263
votes
9 answers

Compare two List objects for equality, ignoring order

Yet another list-comparing question. List list1; List list2; I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list. Is there a…
Bruno Teixeira
  • 2,819
  • 3
  • 14
  • 8
257
votes
12 answers

How can I check if a Perl array contains a particular value?

I am trying to figure out a way of checking for the existence of a value in an array without iterating through the array. I am reading a file for a parameter. I have a long list of parameters I do not want to deal with. I placed these unwanted…
Mel
  • 3,537
  • 4
  • 22
  • 19
237
votes
5 answers

Python None comparison: should I use "is" or ==?

My editor warns me when I compare my_var == None, but no warning when I use my_var is None. I did a test in the Python shell and determined both are valid syntax, but my editor seems to be saying that my_var is None is preferred. Is this the…
Clay Wardell
  • 12,466
  • 11
  • 39
  • 56