8 votes

Implement the `<=>` three-way comparison operator on numbers

polyglot, 3 bytes <=> Try it in Perl! <Insert other languages that already have the exact <=> operator here>
8 votes

Implement the `<=>` three-way comparison operator on numbers

BQN, 2 bytes ×- Try it at BQN REPL × # sign of - # left arg minus right arg BQN, 3 bytes ...
Dominic van Essen's user avatar
7 votes

Implement the `<=>` three-way comparison operator on numbers

Python 2, 3 bytes cmp A built-in function that accepts \$a\$ and \$b\$ and returns \$a \lt = \gt b\$. Try it online!
Jonathan Allan's user avatar
6 votes

Implement the `<=>` three-way comparison operator on numbers

JavaScript (ES6), 17 bytes Expects (a)(b). a=>b=>(a>b)-(a<b) Try it online!
Arnauld's user avatar
  • 192k
5 votes

Implement the `<=>` three-way comparison operator on numbers

Python 3, 22 bytes lambda a,b:(a>b)-(a<b) Try it online! Python 3 lacks the builtin cmp -1 thanks to Dominic van Essen
Jitse's user avatar
  • 7,016
4 votes

Implement the `<=>` three-way comparison operator on numbers

Java (JDK), 14 bytes Float::compare Try it online! Java (JDK), 13 bytes Long::compare Try it online! Only supports integers.
Unmitigated's user avatar
  • 2,749
4 votes

Implement the `<=>` three-way comparison operator on numbers

K (ngn/k), 9 bytes 1&-_1^-%- Try it online! K lacks a sign builtin, which makes this a bit trickier. -%- negative square ...
ovs's user avatar
  • 59.3k
4 votes

Implement the `<=>` three-way comparison operator on numbers

Nibbles, 1.5 bytes (3 nibbles) `$- ...
Dominic van Essen's user avatar
4 votes

Implement the `<=>` three-way comparison operator on numbers

Excel, 12 bytes =SIGN(A1-B1) where A1 and B1 contain a and b respectively.
Jos Woolley's user avatar
  • 1,942
4 votes

Implement the `<=>` three-way comparison operator on numbers

J, 3 bytes *@- Try it online! The 3-byte solution >-< featured in a few other answers also works for the same byte count (...
Conor O'Brien's user avatar
4 votes

Implement the `<=>` three-way comparison operator on numbers

Raku, 8 bytes +(*cmp*) Try it online! cmp is Raku's built-in smart comparison operator, and ...
Sean's user avatar
  • 7,926
4 votes

Implement the `<=>` three-way comparison operator on numbers

Go, 52 51 bytes func(a,b int)int{n:=1 for-1<n&&b-a<n{n--} return-n} Attempt This Online! By @Kevin Cruijssen. Uses a ...
bigyihsuan's user avatar
  • 9,348
4 votes

Implement the `<=>` three-way comparison operator on numbers

Vyxal, 2 bytes Try it Online! Takes input as b then a. Simply subtract the two numbers and get the sign of the result. Alternatively a more fun looking, symbol ...
lyxal's user avatar
  • 33.3k
3 votes

Implement the `<=>` three-way comparison operator on numbers

05AB1E, 2 bytes .S Builtin. Takes the inputs in the order \$b,a\$. Try it online. Explanation: ...
Kevin Cruijssen's user avatar
3 votes

Implement the `<=>` three-way comparison operator on numbers

Kotlin, 16 bytes Float::compareTo Try it online! Annoyingly, longer than Java. Kotlin, 14 bytes Int::compareTo Try it online! ...
Seggan's user avatar
  • 5,317
3 votes

Implement the `<=>` three-way comparison operator on numbers

R, 15 bytes \(a,b)sign(a-b) Attempt This Online!
pajonk's user avatar
  • 16k
3 votes

Implement the `<=>` three-way comparison operator on numbers

HP‑41C series,  9  11 Bytes a needs to be entered first, then b, so a is placed in the Y ...
Kai Burghardt's user avatar
3 votes

Implement the `<=>` three-way comparison operator on numbers

Golfscript, 7 bytes .~>\~<- read from stdin ...
NGeorgescu's user avatar
3 votes

Implement the `<=>` three-way comparison operator on numbers

Burlesque, 2 bytes cm Try it online! Built in compare.
DeathIncarnate's user avatar
3 votes

Implement the `<=>` three-way comparison operator on numbers

PIC16F88x Machine Code, 6 words (84 bits) A function that expects a to be at address 0x70 in memory, and b to be in register W. ...
Bbrk24's user avatar
  • 2,803
3 votes

Implement the `<=>` three-way comparison operator on numbers

K (ngn/k), 5 bytes -/<?, Try it online! , concatenate the inputs <? grade-up the ...
coltim's user avatar
  • 5,903
2 votes

Implement the `<=>` three-way comparison operator on numbers

Pascal, 55 Bytes function f(a,b:real):real;begin f:=ord(a>b)-ord(a<b)end
Kai Burghardt's user avatar
2 votes

Implement the `<=>` three-way comparison operator on numbers

Charcoal, 12 bytes NθNηI⁻›θη‹θη Try it online! Link is to verbose version of code. Explanation: Port of @Arnauld's JavaScript answer. Alternative approach, also 12 ...
Neil's user avatar
  • 170k
2 votes

Implement the `<=>` three-way comparison operator on numbers

Retina 0.8.2, 29 bytes \d+ $* (1*) \1 0 1+0 1 01+ -1 Try it online! Link includes test cases. Only works on non-negative integers. Explanation: ...
Neil's user avatar
  • 170k
2 votes

Implement the `<=>` three-way comparison operator on numbers

MathGolf, 2 bytes Try it online. Explanation: ...
Kevin Cruijssen's user avatar
2 votes

Implement the `<=>` three-way comparison operator on numbers

Pip, 4 bytes aCMb There's a built-in operator for that. (Fun fact: in my original designs for Pip, this operator was <=>, ...
DLosc's user avatar
  • 39.2k
2 votes

Implement the `<=>` three-way comparison operator on numbers

Pyth, 3 bytes ._- Try it online! Defines a function which takes two inputs b, a. Explanation ...
CursorCoercer's user avatar
2 votes

Implement the `<=>` three-way comparison operator on numbers

C (gcc), 22 bytes This is practically @TobySpeight's answer now since he changed the entire algorithm. f(a,b){a=(a>b)-(a<b);} Try it online!
Peter's user avatar
  • 1,007
2 votes

Implement the `<=>` three-way comparison operator on numbers

Julia 1.0, 13 3 bytes cmp Try it online! -10 bytes thanks to MarcMush: use cmp Alternate solution, 13 bytes ...
Ashlin Harris's user avatar
2 votes

Implement the `<=>` three-way comparison operator on numbers

Piet + ascii-piet, 21 bytes (4×7=28 codels) vekabrA krbtkFbks?Tbb Try Piet online! Just a minor variation from my answer to "output the sign". Takes two ...
Bubbler's user avatar
  • 76k

Only top scored, non community-wiki answers of a minimum length are eligible