Bug 151009

Summary: Sorting, oCellRange.Sort(oSortDesc()), 3 columns by column#2 but column#3 sorted
Product: LibreOffice Reporter: Nick <NALong007>
Component: BASICAssignee: Not Assigned <libreoffice-bugs>
Status: RESOLVED WORKSFORME    
Severity: normal CC: andreas.heinisch, buzea.bogdan, rafael.palma.lima
Priority: medium    
Version: 7.3.2.2 release   
Hardware: All   
OS: Windows (All)   
Whiteboard:
Crash report or crash signature: Regression By:
Attachments: Test file and macros as requested

Description Nick 2022-09-16 20:42:49 UTC
Description:
Column#1 & 2 are numeric, column#3 alphanumeric. When sorting by column#2 using a sort range of the 3 coulumns, column#3 is sorted. If the sort range is 2 columns, column2 is sorted.

Steps to Reproduce:
1.Create table with 3 columns, #1 numeric, #2 numeric and #3 alphanumeric in columns D, E & C
2.Create macro using https://wiki.documentfoundation.org/Macros/Calc/ba023
3.Set oSortFields(0).Field = 4 rem column E
run macro

Actual Results:
5	4	A
1	5	B
2	1	C
3	2	D
4	3	E



Expected Results:
2	1	C
3	2	D
4	3	E
5	4	A
1	5	B



Reproducible: Always


User Profile Reset: No



Additional Info:
If you put the same data in columns A, B & C, the expected results are obtained!
Comment 1 Rafael Lima 2022-09-16 21:17:56 UTC
Can you share a sample file with the data you used for testing as well as with your implementation of the macro?
Comment 2 Nick 2022-10-04 15:56:03 UTC
Created attachment 182832 [details]
Test file and macros as requested

I made an error in my original report. Sorting B in A, B, & C works but sorting E in D, E & F does not work. F is sorted.

Hope this helps.
Comment 3 Tammy 2022-11-27 17:50:45 UTC
At first, I could reproduce the issue in which columns C, D, and E did not work in sorting the columns. When I changed the oSortFields(0).Field = 4 to oSortFields(0).Field = 1 for test 2 it seems to work to show the sorted column 2. 
Version: 7.4.2.3 (x64) / LibreOffice Community
Build ID: 382eef1f22670f7f4118c8c2dd222ec7ad009daf
CPU threads: 8; OS: Windows 10.0 Build 22000; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: threaded
Comment 4 Nick 2022-11-28 16:51:28 UTC
Thanks for your input. You confirmed the bug I was reporting

The macro Test1 sorts colmuns A, B & C by index 1 (column B) correctly.

The macro Test2 is supposed to sort columns D, E & F by index 4 (column E) but it does not.

Columns D, E & F are equal to columns A, B & C.

So the question is "Why does sorting by index 1 for A, B and C work but sorting by index 4 for D, E & F which should produce the same result but it does not?"
Comment 5 Andreas Heinisch 2023-01-02 11:56:03 UTC
The Field of the oSortFields variable is not counted from the beginning of a sheet, but counted relatively from the selected range.

So, if you want to sort the data contained in column E, you don't have to specify the Field parameter from the beginning of the sheet, but from the beginning of the range:

REM Range from D to F
sText2 = "$D$1:$F$5"
oCellRange = oSheet.getCellRangeByName(sText2)

REM Sort by Column E descending (D has index 0, E has index 1, and F has index 2)
oSortFields(0).Field = 1
oSortFields(0).SortAscending = TRUE
oSortFields(0).FieldType = com.sun.star.util.SortFieldType.NUMERIC

Imho, this needs some clarification in the documentation.
Comment 6 Nick 2023-01-02 20:15:21 UTC
Many thanks!!! In more than my humble opinion, the documentations NEEDS to be changed to show the index is from the the specified range. As far as I know, any other index is specified from the sheet.  Note that Tammy found the same problem I did by using the sheet index.

Again, thank you.
Comment 7 Andreas Heinisch 2023-01-03 10:34:27 UTC
I updated the description in https://wiki.documentfoundation.org/Macros/Calc/ba023
Comment 8 Nick 2023-01-03 12:03:14 UTC
SUPER! Thanks.