What is the easiest way to reverse the items in a VBA collection?
reverse() method of Collections class as the name itself suggests is used for reversing elements been there up in the object in which they are stored. It reverses the order of elements in a list passed as an argument. Parameter: Object of a class whose elements are to be reversed.
The reverse function of the Collections class is used to reverse the order of elements in a specified list. Collections is defined in the util package in Java, so you must import the util package before you can use the reverse function, as shown below. import java. util.
The reverseOrder() method of Collections class that in itself is present inside java. util package returns a comparator and using this comparator we can order the Collection in reverse order.
Now, when it comes to using slicing for reversing a list, you would need to use the reverse slicing operator [::-1] syntax. This sets the step to -1 and gets all items in the list in reverse order. The slicing operator does not modify the original list.
Don't know of any neat way of doing it but you could do something like (code not tested):
Dim MyNewCol as New Collection
For Each obj in MyCol
If MyNewCol.Count > 0 Then
MyNewCol.Add item := obj, before := 1
Else
MyNewCol.Add item := obj
End If
Next
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With