繁体   English   中英

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

[英]How to sort according to key in an Ordered Collection in Smalltalk

我试图通过其键对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. 

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM