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
3234
votes
52 answers

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: 'Prentice' } ]; How can I sort them by the value of…
Tyrone Slothrop
  • 32,631
  • 3
  • 14
  • 8
2567
votes
43 answers

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

Is there a universal JavaScript function that checks that a variable has a value and ensures that it's not undefined or null? I've got this code, but I'm not sure if it covers all cases: function isEmpty(val){ return (val === undefined || val ==…
Alex
  • 29,612
  • 11
  • 60
  • 134
1245
votes
10 answers

Object comparison in JavaScript

What is the best way to compare objects in JavaScript? Example: var user1 = {name : "nerd", org: "dev"}; var user2 = {name : "nerd", org: "dev"}; var eq = user1 == user2; alert(eq); // gives false I know that two objects are equal if they refer to…
spankmaster79
  • 19,078
  • 10
  • 38
  • 69
1220
votes
15 answers

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

I've got a Python program where two variables are set to the value 'public'. In a conditional expression I have the comparison var1 is var2 which fails, but if I change it to var1 == var2 it returns True. Now if I open my Python interpreter and do…
jottos
  • 19,028
  • 9
  • 28
  • 26
856
votes
12 answers

In Bash, how can I check if a string begins with some value?

I would like to check if a string begins with "node" e.g. "node001". Something like if [ $HOST == user* ] then echo yes fi How can I do it correctly? I further need to combine expressions to check if HOST is either "user1" or begins with…
Tim
  • 1
  • 122
  • 314
  • 481
727
votes
25 answers

What is the Difference Between Mercurial and Git?

I've been using git for some time now on Windows (with msysGit) and I like the idea of distributed source control. Just recently I've been looking at Mercurial (hg) and it looks interesting. However, I can't wrap my head around the differences…
Spoike
  • 112,352
  • 42
  • 133
  • 155
724
votes
27 answers

How to test multiple variables against a single value?

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0 : …
user1877442
  • 7,431
  • 3
  • 11
  • 5
671
votes
20 answers

How do I check for null values in JavaScript?

How can I check for null values in JavaScript? I wrote the code below but it didn't work. if (pass == null || cpass == null || email == null || cemail == null || user == null) { alert("fill all columns"); return false; } And…
Mahdi_Nine
  • 11,985
  • 25
  • 78
  • 112
659
votes
7 answers

When to use CouchDB over MongoDB and vice versa

I am stuck between these two NoSQL databases. In my project I will be creating a database within a database. For example, I need a solution to create dynamic tables. So users can create tables with columns and rows. I think either MongoDB or…
Luke101
  • 56,845
  • 75
  • 204
  • 330
637
votes
11 answers

How do I do a case-insensitive string comparison?

How can I do case insensitive string comparison in Python? I would like to encapsulate comparison of a regular strings to a repository string using in a very simple and Pythonic way. I also would like to have ability to look up values in a dict…
Kozyarchuk
  • 18,211
  • 13
  • 38
  • 46
570
votes
4 answers

String comparison in Python: is vs. ==

I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was while line is not ''. Running through it in the debugger, it turned out that line was in fact ''. When I changed it to…
Coquelicot
  • 7,727
  • 4
  • 28
  • 35
567
votes
7 answers

What's the difference between equal?, eql?, ===, and ==?

I am trying to understand the difference between these four methods. I know by default that == calls the method equal? which returns true when both operands refer to exactly the same object. === by default also calls == which calls equal?... okay,…
denniss
  • 15,528
  • 26
  • 84
  • 135
460
votes
19 answers

Compare two files in Visual Studio

I saw new comparsion tool in VS 2012 for comparing two files or two versions of file. I like it. But when I tried to find it I can't because I don't use TFS. Is there a way how I can just compare two files with the built-in feature in VS (but…
Libor Zapletal
  • 12,142
  • 18
  • 79
  • 165
448
votes
3 answers

Optimum way to compare strings in JavaScript?

I am trying to optimize a function which does binary search of strings in JavaScript. Binary search requires you to know whether the key is == the pivot or < the pivot. But this requires two string comparisons in JavaScript, unlike in C like…
HRJ
  • 15,190
  • 10
  • 49
  • 75
447
votes
24 answers

Check if all elements in a list are identical

I need a function which takes in a list and outputs True if all elements in the input list evaluate as equal to each other using the standard equality operator and False otherwise. I feel it would be best to iterate through the list comparing…
max
  • 40,904
  • 40
  • 170
  • 328
1
2 3
99 100