SortedCollection.st
changeset 16000 74d0c38d1834
parent 14310 b1787ec39fd8
child 16002 5fd1486ee60a
--- a/SortedCollection.st	Wed Feb 12 15:33:29 2014 +0100
+++ b/SortedCollection.st	Wed Feb 12 15:33:40 2014 +0100
@@ -330,15 +330,27 @@
 !SortedCollection methodsFor:'accessing'!
 
 largest:n
-    "return the n largest elements"
+    "return a collection containing the n largest elements, largest last.
+     Raises an exception, if the receiver does not contain at least n elements"
+
+    |mySize|
 
+    mySize := self size.
+    n > mySize ifTrue:[
+        self notEnoughElementsError
+    ].
     sortBlock == DefaultSortBlock ifTrue:[
-        ^ self copyFrom:(self size-n+1)
+        ^ self copyFrom:(mySize-n+1)
     ].
+    "/ do not trust the sortblock to sort small-to-large
     ^ super largest:n
 
     "
-     #(10 35 20 45 30 5) asSortedCollection largest:3 
+     #(10 35 20 45 30 5) asSortedCollection largest:3  
+     (#(10 35 20 45 30 5) asSortedCollection:[:a :b | a > b]) largest:3    
+
+     #(10 35 20 45 30 5) asSortedCollection largest:6  
+     #(10 35 20 45 30 5) asSortedCollection largest:7  
     "
 !
 
@@ -990,11 +1002,12 @@
 !SortedCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.76 2012-08-10 19:28:52 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.77 2014-02-12 14:33:40 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.76 2012-08-10 19:28:52 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.77 2014-02-12 14:33:40 cg Exp $'
 ! !
 
+
 SortedCollection initialize!