diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index f45dc9fb..263d22b4 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -5157,6 +5157,54 @@ DataFrameTest >> testSortByAll [ self assert: actual equals: expected ] +{ #category : #tests } +DataFrameTest >> testSortByRowNames [ + + | dataFrame expected sortedDF | + dataFrame := DataFrame withRows: + #( #( 'CD' 3 2 ) + #( 'AC' 1 4 ) + #( 'DB' 2 4 ) + #( 'BA' 5 1 ) ). + dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ). + + expected := DataFrame withRows: + #( #( 'AC' 1 4 ) + #( 'BA' 5 1 ) + #( 'CD' 3 2 ) + #( 'DB' 2 4 ) ). + expected rowNames: #( 'AC' 'BA' 'CD' 'DB' ). + + sortedDF := dataFrame sortByRowNames. + + self assert: sortedDF equals: expected +] + +{ #category : #tests } +DataFrameTest >> testSortByRowNamesUsing [ + "sorts by second letter of row name" + + | dataFrame expected sortedDF | + dataFrame := DataFrame withRows: + #( #( 'CD' 3 2 ) + #( 'AC' 1 4 ) + #( 'DB' 2 4 ) + #( 'BA' 5 1 ) ). + dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ). + + expected := DataFrame withRows: + #( #( 'BA' 5 1 ) + #( 'DB' 2 4 ) + #( 'AC' 1 4 ) + #( 'CD' 3 2 ) ). + expected rowNames: #( 'BA' 'DB' 'AC' 'CD' ). + + sortedDF := dataFrame sortByRowNamesUsing: [ :name1 :name2 | + name1 second <= name2 second ]. + + self assert: sortedDF equals: expected +] + { #category : #tests } DataFrameTest >> testSortByUsing [ "Sort by second letter of city name" @@ -5224,6 +5272,29 @@ DataFrameTest >> testSortDescendingByAll [ self assert: actual equals: expected ] +{ #category : #tests } +DataFrameTest >> testSortDescendingByRowNames [ + + | dataFrame expected sortedDF | + dataFrame := DataFrame withRows: + #( #( 'CD' 3 2 ) + #( 'AC' 1 4 ) + #( 'DB' 2 4 ) + #( 'BA' 5 1 ) ). + dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ). + + expected := DataFrame withRows: + #( #( 'DB' 2 4 ) + #( 'CD' 3 2 ) + #( 'BA' 5 1 ) + #( 'AC' 1 4 ) ). + expected rowNames: #( 'DB' 'CD' 'BA' 'AC' ). + + sortedDF := dataFrame sortDescendingByRowNames. + + self assert: sortedDF equals: expected +] + { #category : #tests } DataFrameTest >> testToColumnApplyElementwise [ diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 0ee13b85..0b09d60d 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2469,6 +2469,29 @@ DataFrame >> sortByAll: arrayOfColumnNames [ ^ self ] +{ #category : #sorting } +DataFrame >> sortByRowNames [ + "Sorts the rows of the data frame based on the row names in ascending order" + + self sortByRowNamesUsing: [ :a :b | a <= b ] +] + +{ #category : #sorting } +DataFrame >> sortByRowNamesUsing: aBlock [ + "Sorts the rows of the data frame based on the row names using the given comparison block" + + | sortedKeys newContents | + sortedKeys := self rowNames sorted: aBlock. + + newContents := DataFrameInternal new: self dimensions. + + sortedKeys withIndexDo: [ :key :i | + newContents rowAt: i put: (self row: key) asArray ]. + + contents := newContents. + self rowNames: sortedKeys +] + { #category : #sorting } DataFrame >> sortDescendingBy: columnName [ "Rearranges the rows of the data frame in descending order of the values in the column named columnName" @@ -2493,6 +2516,13 @@ DataFrame >> sortDescendingByAll: arrayOfColumnNames [ ^ self ] +{ #category : #sorting } +DataFrame >> sortDescendingByRowNames [ + "Sorts the rows of the data frame based on the row names in descending order" + + self sortByRowNamesUsing: [ :a :b | a >= b ] +] + { #category : #statistics } DataFrame >> stdev [ "Standard deviation is a measure of how dispersed the data is in relation to the average"