11

I'm new using Pharo and I'm trying to iterate over an OrderedCollection, but starting from the end.

For example:

| c |
c := OrderedCollection new.
c add: (1).
c add: (2).
c add: (3).
c do: [ :each | Transcript show: each ; cr ]

The result is 1 2 3 but I want 3 2 1.

Is it possible or do I have to use another kind of collection?

Kwaku
  • 276
  • 2
  • 15
Carlos Rivero
  • 342
  • 2
  • 17

1 Answers1

17

To iterate an OrderedCollection in reverse order you can use the reverseDo: method, e.g.

c reverseDo: [ :each | Transcript show: each; cr ].
halfer
  • 19,824
  • 17
  • 99
  • 186