Ordenar en Smalltalk
asSortedCollection ordena; el bloque de orden elige cómo.
"asSortedCollection orders; a sort block chooses how."
| runs bySize |
runs := #(340 85 500 120).
Transcript show: runs asSortedCollection asArray printString; cr.
bySize := runs asSortedCollection: [:a :b | a > b].
Transcript show: 'Biggest first: ', bySize asArray printString; cr.
"sorted: works straight on sequenceable collections."
Transcript show: (#('folio' 'quarto' 'octavo')
sorted: [:a :b | a size < b size]) printString; cr.
Transcript show: (runs asSortedCollection: [:a :b | a > b])
first printString; cr.
Cómo funciona
asSortedCollectionpor defecto va ascendente.- Un bloque de dos argumentos como
[:a :b | a > b]lo invierte. sorted:funciona directo sobre arreglos.
El intento, en números
- Líneas
- 14
- Caracteres a escribir
- 522
- Tokens
- 95
- Ritmo de tres estrellas
- 55 tpm
Al ritmo de tres estrellas de 55 tokens por minuto, este intento toma unos 104 segundos.
Paso 1 de 3 en Orden e identidad; paso 21 de 31 en Fundamentos del lenguaje.