Using the Less Than Comparison Operator for Strings

Using the less than comparison operator for strings

The less-than operator on strings does a lexicographical comparison on the strings. This compares strings in the same way that they would be listed in dictionary order, generalized to work for strings with non-letter characters.

For example:

"a" < "b"
"a" < "ab"
"A" < "a" (Since A has ASCII value 65; a has a higher ASCII value)
"cat" < "caterpillar"

For more information, look at the std::lexicographical_compare algorithm, which the less-than operator usually invokes.

As for -= and *=, neither of these operators are defined on strings. The only "arithmetic" operators defined are + and +=, which perform string concatenation.

Hope this helps!

Compare two strings with '' and '' operators in JavaScript

As said above, the formal specification is in the standard: http://www.ecma-international.org/ecma-262/7.0/#sec-abstract-relational-comparison , in layman's terms the logic is like this:

1) String vs String

Split both strings into 16-bit code units and compare them numerically. Note that code units != characters, e.g. "cafè" < "cafè" is true (really).

2) String vs other primitive

Convert both to numbers. If one of them is NaN, return false, otherwise compare numerically. +0 and -0 are considered equal, +/-Infinity is bigger/smaller than anything else.

3) String vs Object

Try to convert the object to a primitive, attempting, in order, [Symbol.toPrimitive]("number"), valueOf and toString. If we've got string, proceed to 1), otherwise proceed to 2). For arrays specifically, this will invoke toString which is the same as join.

How comparison operator for strings works in C++, if strings are numbers?

The behaviour of your code is undefined.

The const char[] literals you have entered decay to const char* pointers for the purpose of comparison.

And the behaviour of the comparison operators on pointers is only defined if the pointers are part of the same array; which yours are not.

If you suffix the literals with an s, e.g.
"3"s then C++14 onwards will treat that as a std::string and will perform a lexographic comparison.

Using the less than comparison operator for strings in F#

This is very similar to the C# equivalent described here.

Indeed if we look at linq2db ExpressionEqualityComparer
they point to a CompareBinary

whilst SQLProvider SqlRuntime.Patterns is pointing to a ConditionOperator.GreaterThan

| ExpressionType.GreaterThan,        (:? BinaryExpression as ce) -> Some (ConditionOperator.GreaterThan,  ce.Left,ce.Right)

That could be (see @Abel comment)

a bug in the query translation, as string implements IComparable

I've solved this by switching to C# (that is more robust and prevents the runtime exception with a compile error, coherent with its syntax, as opposite to what happens in F#) and linq2db (that is also more mature and robust than F# SQL type provider)

Less Than operator on string comparison yields same result no matter the situation

It's because a string literal gives you a pointer to a read-only array containing the string (plus its terminator). What you are comparing are not the strings but the pointers to these strings.

Use std::strcmp if you want to compare C-style strings. Or use std::string which have overloaded comparison operators defined.

Using Comparison ( ) Operators With Non-Numeric Strings in Javascript

When using comparison operators with operands of different types, primitive values are coerced as numbers. In this case, for the purpose of comparison "y" becomes NaN, which is neither greater than, less than or equal to any number.

comparing strings with or operators (C)

With the indirection operator * you are actually comparing the values of the characters the pointers point to at the moment of the dereference.

So in your code, it's comparing 's' to 'a' as follows 's' > 'a' which is true.

The values have char type and hence it's well defined to use the <, > ==, >=, <=, != operators.

Be careful when declaring a pointer to a string literal, use the const qualifier to prevent accidentally modifying it because that would be undefined.

What is the meaning of comparing strings with =

All comparisons of std::string are lexicographical. See std::basic_string::operator>=.

You can find an excellent answer which explains this in detail here:
Using the less than comparison operator for strings. The operators < and >= are not equivalent, but the principle is the same.



Related Topics



Leave a reply



Submit