Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #60004

Rosetta: Sort an array of composite structures

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject Rosetta: Sort an array of composite structures
Date 2017-05-29 05:48 +0000
Organization Aioe.org NNTP Server
Message-ID <oggcn0$tei$1@gioia.aioe.org> (permalink)

Show all headers | View raw


> Sort an array of composite structures by a key.


a =
[["gold", "shiny"],
 ["neon", "inert"],
 ["sulphur", "yellow"],
 ["iron", "magnetic"],
 ["zebra", "striped"],
 ["star", "brilliant"],
 ["apple", "tasty"],
 ["ruby", "red"],
 ["dice", "random"],
 ["coffee", "stimulating"],
 ["book", "interesting"]]


puts a.sort.map{|xs| xs.join " => "}

 ===>
apple => tasty
book => interesting
coffee => stimulating
dice => random
gold => shiny
iron => magnetic
neon => inert
ruby => red
star => brilliant
sulphur => yellow
zebra => striped


puts a.sort_by(&:last).map{|xs| xs.join " => "}

 ===>
star => brilliant
neon => inert
book => interesting
iron => magnetic
dice => random
ruby => red
gold => shiny
coffee => stimulating
zebra => striped
apple => tasty
sulphur => yellow

In Forth?

-- 
New York City: Without Blacks and Mestizos, 90% of Crime Would Disappear.
Actually, the real figures would be even more startling, because Middle
Easterners are falsely categorized as White in the statistics.
nationalvanguard.org/2015/10/new-york-city-without-blacks-and-mestizos-90-of-crime-would-disappear/

Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread


Thread

Rosetta: Sort an array of composite structures "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-29 05:48 +0000

csiph-web