How are strings compared?

From the docs: The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. Also: Lexicographical ordering for strings uses the Unicode code point number … Read more

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

When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits: Integer b2 =Integer.valueOf(127) This line of code is also generated when you use autoboxing. valueOf is implemented such that certain numbers are “pooled”, and it returns the same instance for values smaller than 128. From the … Read more

How to compare dates in Java? [duplicate]

Date has before and after methods and can be compared to each other as follows: if(todayDate.after(historyDate) && todayDate.before(futureDate)) { // In between } For an inclusive comparison: if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) { /* historyDate <= todayDate <= futureDate */ } You could also give Joda-Time a go, but note that: Joda-Time is the de facto standard … Read more

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

I was a member of the IEEE-754 committee, I’ll try to help clarify things a bit. First off, floating-point numbers are not real numbers, and floating-point arithmetic does not satisfy the axioms of real arithmetic. Trichotomy is not the only property of real arithmetic that does not hold for floats, nor even the most important. … Read more

How do I do a case-insensitive string comparison?

Assuming ASCII strings: string1 = ‘Hello’ string2 = ‘hello’ if string1.lower() == string2.lower(): print(“The strings are the same (case insensitive)”) else: print(“The strings are NOT the same (case insensitive)”) As of Python 3.3, casefold() is a better alternative: string1 = ‘Hello’ string2 = ‘hello’ if string1.casefold() == string2.casefold(): print(“The strings are the same (case insensitive)”) … Read more

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

From the python 2 manual: CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for … Read more

Sorting in JavaScript: Shouldn’t returning a boolean be enough for a comparison function?

TL;DR I have always successfully sorted my arrays like this No, you have not. And didn’t notice it. A quick counterexample: > [1,1,0,2].sort(function(a, b){ return a>b }) Array [0, 1, 2, 1] // in Opera 12. Results may vary between sorting algorithm implementations why? Because your comparison function does return false (or 0, equivalently) even … Read more