If you are using Scala 2.8, you can use the sortWith method of the ArrayBuffer[T] class, which inherits from the SeqLike attribute.
The following code snippets sorts an ArrayBuffer[T] object in ascending order:
def ascendingSort[T <% Ordered[T]](xs: ArrayBuffer[T]) = xs.sortWith(_ < _)
Note that this does not mutate the actual ArrayBuffer , but creates a new one with the elements in the correct order.
If you are using Scala 2.7, you can use the stableSort method of the stableSort object. This takes ArrayBuffer elements and creates an array of elements sorted in the correct order (specified by closure as an argument, ascending by default).
For instance:
val a = new scala.collection.mutable.ArrayBuffer[Int]() a += 5 a += 2 a += 3 scala.util.Sorting.stableSort(a)
The important question is what you want to do with ArrayBuffer . Typically, Buffer used within different algorithms to improve the performance of intermediate results. If you use it for this, take a look at ways to sort the collection that you want to return at the end of your algorithm. The Sorting object already provides a way to convert an ArrayBuffer to a sorted Array .
From a Buffer class skalyadok:
Buffers are used to gradually create sequences of elements.
Since you are using it with Actor s, you can use it for some lineup of actors - in this case, you can see the Queue collection.
Hope this helps,
- Flavy Chipchigan
source share