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
291
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
  • 3,099
  • 3
  • 16
  • 8
284
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
  • 11,614
  • 13
  • 44
  • 62
278
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,855
  • 4
  • 24
  • 19
240
votes
4 answers

How does tuple comparison work in Python?

I have been reading the Core Python programming book, and the author shows an example like: (4, 5) < (3, 5) # Equals false So, I'm wondering, how/why does it equal false? How does python compare these two tuples? Btw, it's not explained in the…
Paulo
  • 6,982
  • 7
  • 42
  • 56
236
votes
22 answers

How can I compare two floating point numbers in Bash?

I am trying hard to compare two floating point numbers within a Bash script. I have two variables, e.g. let num1=3.17648e-22 let num2=1.5 Now, I just want do a simple comparison of these two numbers: st=`echo "$num1 < $num2" | bc` if [ $st -eq 1];…
Jonas
  • 2,974
  • 4
  • 24
  • 23
224
votes
9 answers

Use '=' or LIKE to compare strings in SQL?

There's the (almost religious) discussion, if you should use LIKE or '=' to compare strings in SQL statements. Are there reasons to use LIKE? Are there reasons to use '='? Performance? Readability?
guerda
  • 23,388
  • 27
  • 97
  • 146
221
votes
15 answers

Best way to compare 2 XML documents in Java

I'm trying to write an automated test of an application that basically translates a custom message format into an XML message and sends it out the other end. I've got a good set of input/output message pairs so all I need to do is send the input…
Mike Deck
  • 18,045
  • 16
  • 68
  • 92
220
votes
12 answers

How to efficiently compare two unordered lists (not sets)?

a = [1, 2, 3, 1, 2, 3] b = [3, 2, 1, 3, 2, 1] a & b should be considered equal, because they have exactly the same elements, only in different order. The thing is, my actual lists will consist of objects (my class instances), not integers.
johndir
  • 2,455
  • 2
  • 15
  • 7
209
votes
31 answers

How do you compare two version Strings in Java?

Is there a standard idiom for comparing version numbers? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be. I need to compare the versions and have the following hold…
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
203
votes
8 answers

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); } } Output: false class D { public static void main(String args[]) { Integer b2=127; …
vipin k.
  • 2,633
  • 5
  • 22
  • 27
196
votes
6 answers

Checking images for similarity with OpenCV

Does OpenCV support the comparison of two images, returning some value (maybe a percentage) that indicates how similar these images are? E.g. 100% would be returned if the same image was passed twice, 0% would be returned if the images were totally…
Boris
  • 8,551
  • 25
  • 67
  • 120
195
votes
18 answers

How to simplify a null-safe compareTo() implementation?

I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): public class Metadata implements Comparable { private String name; private…
Jonik
  • 80,077
  • 70
  • 264
  • 372
187
votes
5 answers

Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128?

I have no idea why these lines of code return different…
DnR
  • 3,487
  • 3
  • 23
  • 31
185
votes
21 answers

Comparing two collections for equality irrespective of the order of items in them

I would like to compare two collections (in C#), but I'm not sure of the best way to implement this efficiently. I've read the other thread about Enumerable.SequenceEqual, but it's not exactly what I'm looking for. In my case, two collections would…
mbillard
  • 38,386
  • 18
  • 74
  • 98
182
votes
2 answers

How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?

The following snippet is annotated with the output (as seen on ideone.com): print "100" < "2" # True print "5" > "9" # False print "100" < 2 # False print 100 < "2" # True print 5 > "9" # False print "5" > 9 …
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623