如何根据Smalltalk中的Ordered Collection中的键进行排序

时间:2018-02-17 20:46:46

标签: smalltalk visualworks

我正在尝试通过其键对OrderedCollection进行排序,但此方法仅返回键。我想获得键和值,但是根据键进行排序。

aAssociation:= Association new.
aAssociation key:6 value:7.
aOrderedCollection:= OrderedCollection new.
aOrderedCollection addFirst: aAssociation.
aAssociation1:= Association new.
aAssociation1 key:5 value:9.
aOrderedCollection addLast: aAssociation1.
aAssociation2:= Association new.
aAssociation2 key:8 value:4.
aOrderedCollection addLast: aAssociation2.
aSortedCollection:= (aOrderedCollection sort: #key ascending) collect:#key. 

1 个答案:

答案 0 :(得分:4)

你正在调用#collect:最后,这是你提取密钥的地方。不要这样做,你就完成了。

也不要调用#sort :,它会修改你发送给它的集合。使用#sorted :,它将返回一个已排序的副本。它也适用于各种收藏品。