简体   繁体   English

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

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

I am trying to sort an OrderedCollection via its keys, but this method returns just the keys. 我试图通过其键对OrderedCollection进行排序,但此方法只返回键。 I want to get both keys and values, but sorted based on keys. 我想获得键和值,但基于键排序。

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. 

You're calling #collect: at the end, which is where you extract the keys. 你正在调用#collect:最后,这是你提取密钥的地方。 Don't do that and you're done. 不要这样做,你就完成了。

Also don't call #sort:, it would modify the collection you send it to. 也不要调用#sort :,它会修改你发送给它的集合。 Use #sorted:, it will return a sorted copy. 使用#sorted :,它将返回一个已排序的副本。 It will also work on all kinds of collections. 它也适用于各种收藏品。

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

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