# Shortest Sorted Hello World

Write a program that takes no input and prints Hello, World! to stdout or your language's closest alternative. The catch is that each line in your program must only contain printable ASCII characters and it must be in lexicographical order, a.k.a. sorted.

Here are all 95 printable ASCII characters in order:

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~  So, for example, the line !!A0~ would be invalid because the A and 0 are out of order. The line !!0A~ would be valid. Each line in your program may be any length and there may be any number of lines. Empty lines are considered sorted. Each of the newlines in your program must be the same (no mixing \n and \r\n). Tabs and other non-printable ASCII characters are forbidden. Due to popular demand, the win condition has been switched around: The submission with the fewest lines wins. Tiebreaker goes to the shortest program (newlines count as single characters). Only Hello, World! and an optional trailing newline should be output. Note that HQ9+ is invalid since it outputs hello, world. I may forbid languages similar to HQ9+ that have one character "Hello, World!" commands due to their triviality. Hint: This is definitely possible in Unary and Lenguage, though not very concisely. • It's a bit late now, but somehow I feel like least lines then fewest bytes might have been more interesting... – Sp3000 Mar 27 '15 at 4:37 • @Sp3000 We'd need a combined measure, or else Unary and Lenguage would mean that only 1-liners could compete. – isaacg Mar 27 '15 at 4:44 • @Sp3000 Maybe, I just wanted to make sure Unary/Lenguage weren't likely to win. I'm open to changing it though (sorry isaacg). Upvote Sp's comment if you agree. – Calvin's Hobbies Mar 27 '15 at 4:46 • @Sp3000 I've taken your advice. See updates. – Calvin's Hobbies Mar 27 '15 at 6:05 • Can the order be inversed? For example, reading from left-to-right? – Ismael Miguel Mar 27 '15 at 11:53 ## 18 Answers # Headsecks, 1 line, 366 bytes $(((((((((((((((((((((((((((((,000000044888<AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADIIIIIIIIIIIILPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPTXXXXXXXXXXXXXXXXXXXXXXXX\diiiiiilqqqqqqqqtyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy|


Headsecks is a trivial Brainfuck substitution where the only thing that matters is the code point modulo 8. The equivalent Brainfuck is merely

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.


Luckily this naive approach just fits. There's some potential for golfing down bytes, but it might be hard.

Tested using this Python interpreter.

## Generating code

code = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------."
command_map = "+-<>.,[]"

last_point = 32
out = []

for char in code:
curr_mod = command_map.index(char)
last_mod = last_point % 8

if curr_mod > last_mod:
last_point += curr_mod - last_mod

elif curr_mod < last_mod:
last_point += (8 - last_mod) + curr_mod

out.append(chr(last_point))

print("".join(out))

• That was clever! +1 – theonlygusti Mar 29 '15 at 15:29
• Could you use a shorter BF program, or is the program built a specific way for a reason? I tried my the BF program I used in your generator, but the output included some unicode. – mbomb007 Mar 30 '15 at 16:29
• @mbomb007 The problem with shorter BF programs is that even this naive method ends at code point 124, so I don't think there's enough wiggle room to introduce new chars... – Sp3000 Mar 30 '15 at 16:47

# ///, 7 lines, 22 bytes

/
//Hello
,
Wor
l
d
!


A rare chance for /// to be competitive (well as long as no one starts with Unary and Lenguage...).

The code first encounters a /, and parses the

/
//


as a substitution instruction which removes all newlines from the remainder of the program. Afterwards, the program merely reads

Hello, World!


which is printed verbatim as it doesn't contain any further slashes.

• I didn't even realize that URLs could contain that many slashes in a row. – Alex A. Mar 27 '15 at 14:35
• @AlexA. It took months to get that URL working, IIRC. We had to keep renaming the article to deal with changes to the server configuration and MediaWiki configuration. – user62131 Nov 19 '16 at 1:16

# JavaScript (6766 62 lines, 227 269 bytes)

(Note: only tested on Firefox 36 and Safari 8, contains minor ES6 features (the Set class))

Z
=
!""+
(0[[]]
+
(
!!""+Set
));c
=Z[~
-22]
$=Z[ 3] $$=Z[ 15]$$$=Z[
24]
=Z[
1]
$=Z[ 0] Set [c + $$+Z[ 5] +Z[ 16] +$$$$+$$$$+Z[ 2] +c +$$$$+$$$+
]
(Z[
14]
+
$$+ +$$$$+$$$$+ "(\ 'H\ "+ +$$+
$$+$$$+ ",\ W\ "+ $$+$$$$+$$+Z[ 6] + "\ !')\ ") ()  The code above basically does: alert("Hello, World!")  Obviously alert is not sorted. So instead we need to generate the statement as a string, and "eval" it: s = "alert('Hello, World!')"; // generate this using sorted code eval(s)  How to generate the string? ES5 supports line continuation so that "AL\ ERT" === "ALERT"  But the character code \ appears before all lowercase letters, so we have to generate the lowercase letters using other methods. We borrow some idea of JSFuck here. The lowercase letters involved in the alert statements are: t e r a o l d  all of these can be extracted from characters of standard objects, which may be expressed in terms of some sorted sequence: t, e, r ← true = !"" a, l ← false = !!"" o ← function = Set d ← undefined = 0[[]]  How do we evaluate the string? Surely we cannot use eval(s) as it is not sorted. Alternatively we could use Function(s)(), but we cannot use Function as it is not sorted either. However, Function is the constructor of all functions, which means Set.constructor === Function. Adding the identifier constructor makes the list of lowercase letters become: t e r a o l d c u n s  which fortunately could still be generated by "truefalseundefinedfunction": t, e, r, u ← true = !"" a, l, s ← false = !!"" o, c, n ← function = Set d ← undefined = 0[[]]  After prettifying, the code above should read like: // lines 1~8 defines our string containing all lowercase letters we want Z = true + (undefined + (false + Set)) // Z = "trueundefinedfalsefunction Set() { [native code] }" // lines 8~20 defines the variables c, $ (e), $$ (l), $$$ (o), //  (r), $ (t)
// for the corresponding lowercase letters extracted from Z

// the rest calls:
// lines 22~36 generates the "constructor" string
// lines 37~61 generates the "alert('Hello, World')" string


Update: Renamed E, L, O, R, T to various repetition of $ to reduce 4 lines. • Absolutely brilliant! I sure don't know if this is possible to use the same time of strategy in Python (it's case sensitive). – mbomb007 Mar 29 '15 at 0:54 • Very nice! Note that =Z[3*7] can be written as =Z[~-22] which only needs one line break instead of two. – me and my cat Mar 30 '15 at 0:14 • @mbomb007 Yeah, but spaces are and they're first in the ASCII set... – Harry Beadle Mar 30 '15 at 9:15 • @meandmycat Thanks! Updated. – kennytm Mar 30 '15 at 10:10 # Insomnia, 4 lines, 59 bytes  FFFFGjnnnnooty FLLddeejkopqyyyyy~~ (<ddjnoppppqrtu <<Fddfj  This program is generated by optimizing for line count. ### 10 lines, 43 bytes (u pt (dppty p~ j <Fptt{ (otz ?o FLu <?FFu~  This above program is generated by optimizing for byte count. # Cygwin bash, 16 lines, 60 bytes This only works due to the case-insensitive file name on Windows, and the fact that Cygwin look up the utilities case-insensitively even if you don't set it to recognize path case-insensitively. A\ =Hello A\ +=\ ", "Wor A\ +=l A\ +=d A\ +=\ ! E\ CHO\$A


Take note of that ECHO at the end.

• I'm not sure if this is 17 or 16 lines (there is a new line at the very end), but either way, this is not going to win anything. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Mar 27 '15 at 14:43
• You won 10 upvotes, that's a lot already. The most upvoted ones (as of now) have 12 upvotes! And the /// solution with 23 upvotes. – Ismael Miguel Mar 30 '15 at 8:52

# ><>, 6 lines, 111 bytes

  """""""/;;Hello
&\o
&++\oooo~~
"++++++++\bbbdlr
+++++\ccccccccfooo}
$++66\cfffffooooopr  I'm having trouble golfing out an extra line, but in any case I'm happy to have beaten the 7-line method. ## Explanation ><> is a stack-based 2D language where instructions are single chars and program flow can be up, down, left or right. The " instruction toggles string parsing, pushing chars until a closing " is met, but because of the nature of ><> there's no such thing as "multi-line string parsing" like with CJam's strings or Python's triple quoted strings. This turned out to be a major problem because starting and ending a line with a " and having other chars in between (e.g. "some chars") is not allowed! Note that / and \ reflect program flow, so we actually execute the first line then all of the lines in reverse order, and most lines are actually executed backwards! [Line 1] """""" Three empty strings, push nothing "/;;Hello " Push "/;;Hello ". Note the wrapping - ><> is toroidal! """""" Three empty strings, push nothing [Line 6] 66++$               Turn the top " " into "," by adding 12, then swap top two
rp                  Reverse stack and pop top 3
ooooo               Print "Hello", stack is now " ,"
fffffc              Push some 15s and a 12

[Line 5]
+++++               Sum the 15s and 12 to give 87, or "W"
}ooo                Move "W" to the back and output ", W"
fcccccccc           Push a 15 and some 12s

[Line 4]
++++++++            Sum the 15 and 12s to give 111, or "o"
"rldbbb\++++++++"   Push chars to stack
r                   Reverse stack
ld                  Push length of stack and 13, not used
bbb                 Push three 11s

[Line 3]
++&                 Sum the 11s to give 33, or "!" and put in register
~~                  Pop top two
oooo                Print "orld"

[Line 2]
&                   Put "!" from register to stack
o                   Print "!"

[Line 1]
;                   Terminate


On a side note, here's an amusing attempt at a ><> least bytes (23 lines, 47 bytes):

v
"
!
d
l
r
o
W

,
o
l
l
e
H
"
/<
l
?
!
;
o
\^

• In the second code you can save 2 bytes thanks to vertical wrap-around: pastebin.com/heqCr1vJ – randomra Mar 28 '15 at 22:57

# CJam, 5 4 lines, 162 bytes

"Hello
")))))))))))))))))))))))))))))))))))68S\cel
"Wor
")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))@SX\c|


Same number of lines as Insomnia! (but a lot more bytes)

## Explanation

"Hello\n"        Push "Hello" and a newline
)                Uncons the newline
)))...)))        Increment the newline into a ","
68S\             Push 68 and a space then swap, leaving 68 on top
c                Convert 68 into a char "D"
el               Lowercase "D" into "d"
"Wor\n"          Push "Wor" and a newline
)                Uncons the newline
)))...)))        Increment the newline into an "l"
@                Rotate top three elements, moving the "d" to the top
SX\              Push a space and 1 then swap, leaving the space on top
c                Convert the space (which is an array) into a char
|                Bitwise or the space (32) with 1 to give "!" (33)


CJam automatically prints the stack afterwards.

Thanks to @Optimizer for reminding me that Sc works, because otherwise SX| fails (it does a setwise or instead, giving the array consisting of a space and 1).

# ><> (Fish), 5 lines, many bytes

The code is extremely long, when I say [6535529689086686142930090 '+' signs] and [6535529689086686142930090 'd' characters] in the first line of the code, I mean that there are literally 6535529689086686142930090 plus signs in a row!

(And for the following lines add the necessary prefix spaces.)

'[6535529689086686142930090 '+' signs]^[6535529689086686142930090 'd' characters]
**,/288
-/:o
%**288:;?\
(1:\


A shorter testable alternative which only prints Hi:

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
**,/288
-/:o
%**288:;?\
(1:\


Method:

• In the first line we create a huge number representing the string in base128.
• In the rest of the lines (going from bottom to top) we print the following character in every loop until the value of the integer reaches 0.
• The looping code is :1(?;:882**%:o-288**,.

(We can print arbitrary 2+ chars long string with this method.)

• Not a valid solution; 6535529689086686142930090 is not ordered. – theonlygusti Mar 29 '15 at 15:23
• @theonlygusti "The code is extremely long, interpret [6535529689086686142930090 '+' signs] and [6535529689086686142930090 'd' characters] literally in the first line of the code." (If unclear what I mean by that check the second piece of code.) – randomra Mar 29 '15 at 16:25
• Oh, okay! Sorry, silly me :P I took it literally. – theonlygusti Mar 29 '15 at 16:29
• Size is 13 YB, roughly 1000 times the size of the Internet. – Charles Mar 30 '15 at 15:37
• @Charles I get ~40YB including the latter lines. – Veedrac Mar 31 '15 at 15:42

# PHP (7 / 17 lines, 36 / 59 bytes):

The browser version? Gone.

But I have 2 solutions, both based on bitwise xor (^) of strings in PHP.

This is an extremely powerful solution! Sometimes, it allows to save plenty of bytes.

Echo
Hello
,AM^mm
,Wor
,OOO^
"#+n
";


I know, it looks awful, but it works!

The next one depends on newlines.

Yes, it uses newlines (\n/UNIX style is required)!

Echo
Bo
.ff
.e
.
"&*]ex
fn
"^
"\n
\n
\n
\n
\n
\n
\n
",B^c
;


The newlines on this one are required to work.

It isn't perfect, but works!

Both answers were based on @MartinBüttner's solution:

Echo
Hello
.
chr
(44
).
chr
(29
+3
).Wor
.l
.d
."!
";


A special thank to @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳, @CJDennis, @MartinBüttner and @skagedal for detecting all my mistakes and for their suggestions.

• The output in the browser is Hello , Wor l d !. Note the extra spaces! This is not a valid answer, unfortunately. – CJ Dennis Mar 30 '15 at 8:13
• Tested in Firefox 36, IE 8, Opera 12.17 & Opera 28 (Chromium) on Windows XP. All rendered the output identically. Since any number of whitespace characters are rendered as a single space character (barring no break space, etc.) each newline gets converted to a space. I would be interested to know if any browser renders your output without spaces! – CJ Dennis Mar 30 '15 at 8:37
• Nice abuse of the language! Since the errors it generates technically go to stderr and not stdout, your code is now valid even though in my case I see a lot of errors in my browser! And, incredibly nit-picky, my PHP setup has short tags off so the first line on my computer is <?php which is invalid! However, I'll forgive you for this! OK, just tested <?Php which works, so I've just learnt that the start tag is case-insensitive! – CJ Dennis Mar 30 '15 at 10:45
• The top two solutions in this answer do not follow the rules. The top one has, as line 8, ."!<br - this is not sorted. The second one has as line 10: ."! - not sorted. – skagedal Mar 30 '15 at 14:27
• @IsmaelMiguel I'm sorry, but no. Line 7 of the first suggestion is not sorted. Lines 5 and 7 of the second suggestion are not sorted. – skagedal Mar 31 '15 at 6:06

# Pyth, 7 lines, 23 bytes

-
"Hello
,
Wor
l
d
!"b


Pretty simple, just use a multiline string and remove the newlines.

Try it here.

• @orlp I added multiline strings a few days ago, and Pyth has had support for newlines in the input for ages. This doesn't use multiline mode at all. Try it on the online compiler, which definitely has no multiline mode. – isaacg Mar 27 '15 at 4:14
• Its almost the same in CJam, with same byte count too. – Optimizer Mar 27 '15 at 6:00

# Forth, 41 lines, 143 bytes

Try it online.

I'm very proud of this. I whipped it out pretty quickly once I found I could do it, having Google searched for case-insensitive languages, and I already know Forth decently. It can almost definitely be shortened, both bytes and lines. Suggestions are appreciated. :D

This program essentially pushes the decimal value of each ASCII character, then prints each one. I attempted to optimize while I wrote it, hence it is less readable than I'd like.

33
99
1
+
DUp
8
+
2DUp
6
+
SWap
11
+
2OVer
SWap
8
9
*
EMit
1
+
EMit
DUp
DUp
EMit
EMit
3
+
EMit
29
3
2DUp
+
EMit
*
EMit
EMit
EMit
EMit
EMit
EMit

• The Emit lines don't work, as i (105) is less than m (109). – bcsb1001 Mar 29 '15 at 19:25
• It should be "EMit", then? – skagedal Mar 30 '15 at 13:33

# Marbelous, 9 lines, 94 bytes

3333
358C
++--29\\
++--3555DE
<<<<<<>>~~
+++++446666D
++++++++../\
++------


Test it online here. Using spaces for blank cells must be checked.

Visual representation of source:

Explanation: Each hexadecimal value in this program moves downward every "tick" (unless it is on \\, in which case it moves to the right). ++ will increment the value, -- decrement, ~~ apply a bitwise not, etc. Values that fall of the bottom are printed as an ASCII character.

For example: The 33 in the top corner becomes (((0x33 +1 +1) << 1) +1 +1 = 0x6C (l).

# Perl, 17 lines

$_ = 'pr int "Hello , Wor l d !"';s ! !!g ;s !. * !$&
!eex


# Rebol - 15 lines (51 bytes)

PRin
AJOin
[
'Hello
Sp
+
12
Sp
'Wor
'l
'd
Sp
+
1
]


Words in Rebol are case-insensitive (eg. PRINT, Print and print would all call the same function).

The above code tidied up:

prin ajoin [
'Hello  sp + 12  sp  'Wor  'l  'd  sp + 1
]


NB. sp returns the space char!

• Your program is invalid, the { can't be before any letters. – theonlygusti Mar 28 '15 at 15:29
• @theonlygusti - Fixed (gone from 9 lines to 13). The only line that caused me a problem was {,}. This is now Sp + 12 (over 3 lines) which is equivalent of space character + 12 (ie. comma) – draegtun Mar 29 '15 at 13:07

# Unary, 1 line, lots of bytes

Every character in this program is 0. The source is too long to put in here, but it consists of a string of this many zeros:

327380789025192647555922325233404088310881092761595127419003295178342329930050528932118548


This is the decimal representation for this binary number:

0b1010010010010010010010010110000010010010010110000010010000010010010000010010010000010001001001001011111000010000010000011110001111001011111000000100000011011011100010010010010010010010100100010010010100000000100001011100001100010010010100011011011011011011100011011011011011011011011100000000010100


Which was created from this BrainF*** program:

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>-[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.


See how I generated it: http://ideone.com/lMT40p

## Batch - 39 Lines (202 bytes)

I wanted to cut down on the size by enabling delayed expansion by running the code inside of a cmd call using the /v on switch, but could not get past all the carats.

@S^
ET^
LO^
C^
AL^
EN^
ABL^
E^
DEL^
AY^
E^
DEX^
P^
ANS^
IO^
N
@S^
ET^
0=Hello
@S^
ET^
1=^
,^
Wor
@S^
ET^
2=l
@S^
ET^
3=d
@E^
CHO^
!^
0^
!!1^
!!2^
!!3^
!^^^
!


Without the newlines (and carats to escape newlines):

@SETLOCAL ENABLEDELAYEDEXPANSION
@SET 0=Hello
@SET 1=, Wor
@SET 2=l
@SET 3=d
@ECHO !0!!1!!2!!3!^^!


# Ruby, 19 lines, 95 bytes

$;=$>
$;<< :Hello$;<<
44::chr
$.= 33:: -1$;<<
$.::chr$;<<
:Wor
$;<< :l$;<<
:d
$;<< 33::chr  Possibly the second-hardest code restriction for Ruby I've seen on this site, after the monstrosity here. Edit for explanation: The only Ruby output method that's sorted is the << method on STDOUT. Unfortunately, the constant for STDOUT is $>, and > is higher ASCII than most symbols, so it's not really possible to call a method on it. We need a multi-line assignment statement to get it into a more tractable variable name, $; (semicolon is still pretty high, but most variables of this form are either non-assignable, or can only be strings). Symbols (:Wor, etc. ) are the easiest way to do a literal, and I don't see a way to strip newlines from a string, so I need multiple print statements. The space and punctuation can't go into a symbol literal, but luckily the chr method on numbers is legal, so I can get a character from its ASCII value. :: is an alternate way of calling methods. Space is tricky because I can't write 32, so I subtract 1 from 33 and assign it to a variable. # Retina, 12 lines, 29 bytes (non-competing) The language is newer than the challenge.  Hello$
,
$Wor$
l
$d$
!

Try it online