Questions tagged [compare]

The analysis required to assess the differences and similarities between two or more entities.

This tag is used to classify a question as one dealing with the comparison of at least two entities. The essence of questions that are tagged should boil down to one of the following questions:

  1. How do I control when the compiler/interpreter thinks that two entities are equal? How do I control when the compiler/interpreter thinks that two entities are unequal?
  2. How do I control when the compiler/interpreter thinks that one entity is bigger than the other?
  3. How do I control when the compiler/interpreter thinks that one entity is smaller than the other?

Note that in some languages, it is possible to define asymmetric inequalities. That is, cases in which a<b does not mean that b<a. This is why in python, there exist separate functions for __gt__ (greater than) and __lt__ (less than), etc

10212 questions
82
votes
4 answers

How do I compare two dictionaries in Swift?

Is there an easy way to compare two [String: AnyObject] dictionaries in swift, since it doesn't accept the == operator? By comparing two dictionaries, I mean checking that they have the same exact keys and for every key they have the same values.
bubakazouba
  • 1,633
  • 2
  • 22
  • 34
77
votes
9 answers

In Python, is there a concise way of comparing whether the contents of two text files are the same?

I don't care what the differences are. I just want to know whether the contents are different.
Corey Trager
  • 22,649
  • 18
  • 83
  • 121
76
votes
11 answers

Check if enum exists in Java

Is there anyway to check if an enum exists by comparing it to a given string? I can't seem to find any such function. I could just try to use the valueOf method and catch an exception but I'v been taught that catching runtime exceptions is not good…
Danny
  • 4,724
  • 6
  • 42
  • 55
73
votes
7 answers

strcmp equivelant for integers (intcmp) in PHP

So we got this function in PHP strcmp(string $1,string $2) // returns -1,0, or 1; We Do not however, have an intcmp(); So i created one: function intcmp($a,$b) { if((int)$a == (int)$b)return 0; if((int)$a > (int)$b)return 1; …
Chase Wilson
  • 1,477
  • 1
  • 12
  • 19
70
votes
13 answers

How would you compare two XML Documents?

As part of the base class for some extensive unit testing, I am writing a helper function which recursively compares the nodes of one XmlDocument object to another in C# (.NET). Some requirements of this: The first document is the source, e.g.…
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
67
votes
9 answers

Dictionary merge by updating but not overwriting if value exists

If I have 2 dicts as follows: d1 = {'a': 2, 'b': 4} d2 = {'a': 2, 'b': ''} In order to 'merge' them: dict(d1.items() + d2.items()) results in {'a': 2, 'b': ''} But what should I do if I would like to compare each value of the two dictionaries and…
siva
  • 2,105
  • 4
  • 20
  • 37
66
votes
5 answers

Why is one string greater than the other when comparing strings in JavaScript?

I see this code from a book: var a = "one"; var b = "four"; a>b; // will return true but it doesn't mention why "one" is bigger than "four". I tried c = "a" and it is smaller than a and b. I want to know how JavaScript compares these strings.
patriot7
  • 663
  • 1
  • 5
  • 5
66
votes
8 answers

Comparing Class Types in Java

I want to compare the class type in Java. I thought I could do this: class MyObject_1 {} class MyObject_2 extends MyObject_1 {} public boolean function(MyObject_1 obj) { if(obj.getClass() == MyObject_2.class) System.out.println("true"); } I…
Carven
  • 14,988
  • 29
  • 118
  • 161
66
votes
5 answers

How to compare Enums in Python?

Since Python 3.4, the Enum class exists. I am writing a program, where some constants have a specific order and I wonder which way is the most pythonic to compare them: class Information(Enum): ValueOnly = 0 FirstDerivative = 1 …
Sebastian Werk
  • 1,568
  • 2
  • 17
  • 30
66
votes
5 answers

How can I use in_array if the needle is an array?

I have 2 arrays, the value will be loaded from database, below is an example: $arr1 = array(1,2,3); $arr2 = array(1,2,3,4,5,6,7); What I want to do is to check if all the values in $arr1 exist in $arr2. The above example should be a TRUE…
Donny Kurnia
  • 5,260
  • 5
  • 35
  • 52
65
votes
4 answers

How to tell if a date is between two other dates?

I have the following codes: if date in (start, end): print('in between') else: print('No!') date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2,…
widget
  • 945
  • 3
  • 13
  • 22
64
votes
10 answers

Efficient way to compare version strings in Java

Possible Duplicate: How do you compare two version Strings in Java? I've 2 strings which contains version information as shown below: str1 = "1.2" str2 = "1.1.2" Now, can any one tell me the efficient way to compare these versions inside strings…
Mike
  • 7,606
  • 25
  • 65
  • 82
64
votes
10 answers

Linq where clause compare only date value without time value

var _My_ResetSet_Array = _DB .tbl_MyTable .Where(x => x.Active == true && x.DateTimeValueColumn <= DateTime.Now) .Select(x => x); Upper query is working correct. But I want to check only date value only. But upper query check…
Frank Myat Thu
  • 4,448
  • 9
  • 67
  • 113
62
votes
9 answers

Difference between period and comma when concatenating with echo versus return?

I just found that this will work: echo $value , " continue"; but this does not: return $value , " continue"; While . works instead of , in both the echo and return statements. What is the difference between a period and a comma here?
omg
  • 136,412
  • 142
  • 288
  • 348
61
votes
5 answers

Compare if two dataframe objects in R are equal?

How do I check if two objects, e.g. dataframes, are value equal in R? By value equal, I mean the value of each row of each column of one dataframe is equal to the value of the corresponding row and column in the second dataframe.
mindless.panda
  • 4,014
  • 4
  • 35
  • 57