Issue 125306 - Sorting not stable from Basic macro
Summary: Sorting not stable from Basic macro
Status: CONFIRMED
Alias: None
Product: Calc
Classification: Application
Component: programming (show other issues)
Version: 4.0.1
Hardware: All All
: P3 Normal (vote)
Target Milestone: ---
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-25 06:05 UTC by dohmasterdoh
Modified: 2014-07-26 19:09 UTC (History)
5 users (show)

See Also:
Issue Type: ENHANCEMENT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments
test csv (900.53 KB, text/csv)
2014-07-25 06:05 UTC, dohmasterdoh
no flags Details

Note You need to log in before you can comment on or make changes to this issue.
Description dohmasterdoh 2014-07-25 06:05:47 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
Comment 1 Andre 2014-07-25 07:30:29 UTC
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.
Comment 2 Oliver Brinzing 2014-07-25 18:16:59 UTC
>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
Comment 3 Regina Henschel 2014-07-25 21:14:08 UTC
You can sort via dispatcher. That is stable.
Comment 4 bmarcelly 2014-07-26 19:09:12 UTC
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