Power Apps Authoring Version: 3.22022.31
I've discovered that for functions 'making a decision' to return results from its arguments (particularly the If and Switch functions), the value type of the returned result does not always agree with that of the argument's result. In fact, only the result from the 1st argument determines the value type for all other arguments' results.
Consequentially, if the function decides to return an argument's result that cannot be converted to the value type of the 1st argument's result, the function returns null/blank. If the argument's result can be converted, the function returns the converted result (never the original result from the argument).
Consider the variable blnDecision in either function of the following examples.
If(blnDecision, 5, "hi"); Switch(blnDecision, true, 5, false, "hi")
When blnDecision is true, each function returns 5. When blnDecision is false, each function returns null/blank.
If(blnDecision, "hi", 5); Switch(blnDecision, true, "hi", false, 5)
When blnDecision is true, each function returns hi. When blnDecision is false, each function returns 5 (a string, not a number).
This inconsistency doesn't seem to be documented anywhere & needs to be fixed. I use decision functions to sort my tables, and I'm unable to sort by number (only sorts by string).
***[EDIT]***
Accepted solution attests that this is not erroneous behavior; it's by-design. The value type of results returned by functions are evaluated on the first execution pass, not every pass. You're stuck with whatever value type you have in a function's 1st argument for all other arguments.
As for my if/switch logic that undesirably passes numbers as strings into the Sort functions, I'll just be concatenating leading 0s to said numbers, formatted in equal character length. That way they sort as 1,2,3,4,5,6,7,8,9,10,11,12,etc. and not 1,10,12,13,14,15,16,17,18,19,2,20,21,22,etc.