Apache OpenOffice (AOO) Bugzilla – Issue 125306
Sorting not stable from Basic macro
Last modified: 2014-07-26 19:09:12 UTC
Created attachment 83738 [details] test csv When sorting from a Basic macro using the following function: Function sort(col As Integer, doc As Object) As Long Dim aSortFields(2) As New com.sun.star.util.SortField Dim aSortDesc(0) As New com.sun.star.beans.PropertyValue Dim count As Long Dim cell As Object Dim oSortRange As Object Sheet = doc.Sheets.GetByName("Sheet1") curs = Sheet.createCursor curs.gotoEndOfUsedArea(True) count = curs.Rows.Count oSortRange = Sheet.getCellRangeByPosition(0,0,20,count) aSortFields(0).Field = col aSortFields(0).SortAscending = TRUE aSortDesc(0).Name = "SortFields" aSortDesc(0).Value = aSortFields() oSortRange.Sort(aSortDesc()) sort() = count end Function the sort is not stable if the column you are sorting is not column 0. Attched is a test csv file with 3 columns. There are 4 rows with a value of "00037CMK3" in column B. The respective values in column C are in the order: 20140402 20140403 20140407 20140409 After sorting by column B, the order of column C becomes: 20140407 20140402 20140403 20140409
Looks like the object to be sorted implements the SheetCellRange and the util::XSortable interface. I did not find a statement regarding stable or unstable sorting so I guess that that is left as implementation detail. If the above is true then this bug is not a defect but a request for enhancement (of the API). Alternatively we could change the implementation from std::sort to std::stable_sort but that might break existing code that expects the sorting to behave like it does.
>After sorting by column B, the order of column C becomes: >20140407 >20140402 >20140403 >20140409 i can confirm this, but if i sort the range via sort dialog, the sort is stable. do we have different sort implementations? lo 4.2.5 behaves same way
You can sort via dispatcher. That is stable.
Your attachment 83738 [details] contains 6467 data lines, followed by 194968 lines of empty data. So the code searching for the End of Used data returns a value for count = 201435. For my test I used only the range A1:C6467 Your code uses the deprecated structure com.sun.star.util.SortField and declares an array of 3 structures, using only one. Stable sort was introduced in OpenOffice.org version 3.1. The following code works, and the sort is stable when you sort on column 1 (column B): Function sort(col As Integer, doc As Object) As Long Dim aSortFields(0) As New com.sun.star.table.TableSortField Dim aSortDesc(0) As New com.sun.star.beans.PropertyValue Dim count As Long Dim cell As Object, Sheet As Object, curs As Object Dim oSortRange As Object Sheet = doc.Sheets.GetByName("Sheet1") ' for simplicity, set range hard coded oSortRange = Sheet.getCellRangeByName("A1:C6467") aSortFields(0).Field = col aSortFields(0).IsAscending = TRUE aSortDesc(0).Name = "SortFields" aSortDesc(0).Value = aSortFields() oSortRange.Sort(aSortDesc()) End Function Conclusion : invalid bug report