Skip to content

Commit 198d818

Browse files
Added DataFrame>> #sortByAll: and #sortDescendingByAll: for chain sorting.
1 parent 81fa574 commit 198d818

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5065,6 +5065,37 @@ DataFrameTest >> testSortBy [
50655065
self assert: actual equals: expected
50665066
]
50675067

5068+
{ #category : #tests }
5069+
DataFrameTest >> testSortByAll [
5070+
5071+
| dataFrame actual expected |
5072+
dataFrame := DataFrame withRows:
5073+
#( #( 2 5 )
5074+
#( 1 2 )
5075+
#( 3 2 )
5076+
#( 1 5 )
5077+
#( 2 4 ) ).
5078+
5079+
dataFrame columnNames: #( season episode ).
5080+
dataFrame rowNames: #( 'A' 'B' 'C' 'D' 'E' ).
5081+
5082+
5083+
expected := DataFrame withRows:
5084+
#( #( 1 2 )
5085+
#( 1 5 )
5086+
#( 2 4 )
5087+
#( 2 5 )
5088+
#( 3 2 ) ).
5089+
5090+
expected columnNames: #( season episode ).
5091+
expected rowNames: #( 'B' 'D' 'E' 'A' 'C' ).
5092+
5093+
actual := dataFrame sortByAll: #( season episode ).
5094+
5095+
5096+
self assert: actual equals: expected
5097+
]
5098+
50685099
{ #category : #tests }
50695100
DataFrameTest >> testSortByUsing [
50705101
"Sort by second letter of city name"
@@ -5101,6 +5132,37 @@ DataFrameTest >> testSortDescendingBy [
51015132
self assert: actual equals: expected
51025133
]
51035134

5135+
{ #category : #tests }
5136+
DataFrameTest >> testSortDescendingByAll [
5137+
5138+
| dataFrame actual expected |
5139+
dataFrame := DataFrame withRows:
5140+
#( #( 2 5 )
5141+
#( 1 2 )
5142+
#( 3 2 )
5143+
#( 1 5 )
5144+
#( 2 4 ) ).
5145+
5146+
dataFrame columnNames: #( season episode ).
5147+
dataFrame rowNames: #( 'A' 'B' 'C' 'D' 'E' ).
5148+
5149+
5150+
expected := DataFrame withRows:
5151+
#( #( 3 2 )
5152+
#( 2 5 )
5153+
#( 2 4 )
5154+
#( 1 5 )
5155+
#( 1 2 ) ).
5156+
5157+
expected columnNames: #( season episode ).
5158+
expected rowNames: #( 'C' 'A' 'E' 'D' 'B' ).
5159+
5160+
actual := dataFrame sortDescendingByAll: #( season episode ).
5161+
5162+
5163+
self assert: actual equals: expected
5164+
]
5165+
51045166
{ #category : #tests }
51055167
DataFrameTest >> testToColumnApplyElementwise [
51065168

src/DataFrame/DataFrame.class.st

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,13 +2197,31 @@ DataFrame >> sortBy: columnName using: aBlock [
21972197
self rowNames: sortedKeys
21982198
]
21992199

2200+
{ #category : #sorting }
2201+
DataFrame >> sortByAll: arrayOfColumnNames [
2202+
" Chain sorts the data frame in ascending order. The data frame is sorted based on the first column in the array of column names, if there are same values, then it sorts these same values based on the values of the second column and so on.."
2203+
2204+
arrayOfColumnNames reverseDo: [ :columnName |
2205+
self sortBy: columnName using: [ :a :b | a <= b ] ].
2206+
^ self
2207+
]
2208+
22002209
{ #category : #sorting }
22012210
DataFrame >> sortDescendingBy: columnName [
22022211
"Rearranges the rows of the data frame in descending order of the values in the column named columnName"
22032212

22042213
self sortBy: columnName using: [ :a :b | a >= b ]
22052214
]
22062215

2216+
{ #category : #sorting }
2217+
DataFrame >> sortDescendingByAll: arrayOfColumnNames [
2218+
" Chain sorts the data frame in descending order. The data frame is sorted based on the first column in the array of column names, if there are same values, then it sorts these same values based on the values of the second column and so on.."
2219+
2220+
arrayOfColumnNames reverseDo: [ :columnName |
2221+
self sortBy: columnName using: [ :a :b | a >= b ] ].
2222+
^ self
2223+
]
2224+
22072225
{ #category : #statistics }
22082226
DataFrame >> stdev [
22092227
"Standard deviation is a measure of how dispersed the data is in relation to the average"

0 commit comments

Comments
 (0)