FileSorter.st
changeset 2174 d1022a910535
parent 2135 51ef149d010d
child 2497 67f066362736
--- a/FileSorter.st	Tue Jun 23 22:38:56 2009 +0200
+++ b/FileSorter.st	Tue Jun 30 15:27:51 2009 +0200
@@ -110,54 +110,61 @@
 
 sortItemList:aList
 
-    |selectorSymbol instanceSortBlock cmpOp sortBlock locSortReverse|
+    |selectorSymbol instanceSortBlock cmpOp sortBlock locSortReverse convertToString caseLessNameString |
 
     selectorSymbol := selector asSymbol.
     sortReverse ifTrue:[
-        cmpOp := #'>'
+        cmpOp := #>.
     ] ifFalse:[
-        cmpOp := #'<'
+        cmpOp := #<.
     ].
     locSortReverse := self sortReverse.
+    caseLessNameString := sortCaseless and:[selector = #baseName].
+
     instanceSortBlock := [:a :b | 
         |entry1 entry2|
 
-        entry1 := (a perform:selectorSymbol).
-        entry2 := (b perform:selectorSymbol).
-        ((entry1 isNil) or:[entry2 isNil]) ifTrue:[
-            ((entry1 isNil) and:[entry2 isNil]) ifTrue:[
+        entry1 := a perform:selectorSymbol.
+        entry2 := b perform:selectorSymbol.
+        (entry1 isNil or:[entry2 isNil]) ifTrue:[
+            (entry1 isNil and:[entry2 isNil]) ifTrue:[
                 true
             ] ifFalse:[
-                ((entry1 notNil) and:[entry2 isNil]) ifTrue:[
+                (entry1 notNil and:[entry2 isNil]) ifTrue:[
                     locSortReverse
                 ] ifFalse:[
                     locSortReverse not
                 ]
             ]
         ] ifFalse:[
-            (entry1 isNumber and:[entry2 isNumber]) ifFalse:[
+            convertToString isNil ifTrue:[
+                "if we have to convert the first pair of values, convert every pair"
+                convertToString := ((entry1 isNumber and:[entry2 isNumber]) 
+                                    or:[(entry1 respondsTo:cmpOp) and:[entry2 respondsTo:cmpOp]]) not.
+            ].
+            convertToString ifTrue:[
                 entry1 := entry1 asString.
                 entry2 := entry2 asString.
             ].
-            ((selector = #baseName) and:[sortCaseless]) ifTrue:[
+            caseLessNameString ifTrue:[
                 entry1 := entry1 asLowercase.
                 entry2 := entry2 asLowercase.
             ].
             entry1 perform:cmpOp with:entry2
         ]
     ].
+
     directoriesBeforeFiles ifTrue:[
         sortBlock := [:a :b|
-            |aIsDir bIsDir res|
+            |aIsDir|
 
             aIsDir := a isDirectory.
-            bIsDir := b isDirectory.
-            (aIsDir ~~ bIsDir) ifTrue:[
-                res := aIsDir 
+            (aIsDir ~~ b isDirectory) ifTrue:[
+                "one is a directory, the other is not"
+                aIsDir 
             ] ifFalse:[
-                res := instanceSortBlock value:a value:b.
+                instanceSortBlock value:a value:b.
             ].
-            res
           ].
     ] ifFalse:[
         sortBlock := instanceSortBlock.
@@ -170,5 +177,5 @@
 !FileSorter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FileSorter.st,v 1.6 2009-05-03 15:33:39 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FileSorter.st,v 1.7 2009-06-30 13:27:51 stefan Exp $'
 ! !