I have two numbers: a and b. I want to put the smaller of them in one variable, and the larger in another variable (if they are equal, then any of them can be in any of the variables). Currently my code (in Javascript / Java) takes 7 lines:
if (a<=b) {
small = a;
large = b;
} else {
small = b;
large = a;
}
I could also use the Math.max and Math.min functions:
small = Math.min(a,b);
large = Math.max(a,b);
This takes only 2 lines but it is wasteful because the test of which one is larger will be done twice - once in Math.min and once in Math.max.
Is there a shorter / more elegant way to do this?