Home » excel » Why is Male > Female equal to TRUE in R, Excel, and VBA? How about in other languages?

# Why is Male > Female equal to TRUE in R, Excel, and VBA? How about in other languages?

Questions:

I’m currently taking the course: Introduction to R on DataCamp and in one exercise (Battle of the sexes) there is an instruction like this:

Read the code in the editor and click ‘Submit Answer’ to test if `male`
is greater than (`>`) `female`

The above instruction inspired me to test the following code in RStudio:

``````'Male' > 'Female'
``````

To my surprise, R gave me the output `TRUE`! I also tried in Excel and VBA, and both came up with outputs `TRUE`, too! Now, I begin to think that they’re programming languages with sexist views (Just kidding, hehe…).

So I wonder, what really happened here? Could anyone here explain it to me? Does this hold `TRUE`, too, for other programming languages? Why?

For R, see `help('>')` or its documentation here, and the wikipedia link about collation:

“Comparison of strings in character vectors is lexicographic within the
strings using the collating sequence of the locale in use: see
locales. The collating sequence of locales such as en_US is normally
different from C (which should use ASCII) and can be surprising.
Beware of making any assumptions about the collation order: e.g. in
Estonian Z comes between S and T, and collation is not necessarily
character-by-character – in Danish aa sorts as a single letter, after
z.”

So summarizing; in your locale, the value of ‘F’ is smaller than the value of ‘M’ in the collation sequence, and thus `Mxxx` is larger than `Fyyy`.

In other languages like C# you can’t compare strings with

``````   "Male" > "Female"
``````

VBA for example converts the first Letter to ASCII and then compares it.

``````MsgBox Asc("male") '= 109
MsgBox Asc("female") '= 102
MsgBox Asc("Male") '= 77
MsgBox Asc("Female") '= 70
``````

This is why it says `"male" > "female" is true`. But `"Male" > "female" is false`.

For the other languages it will be similiar

``````b = “3”