1. In a spreadsheet, create a rectangular data block with all cells having only text (no numbers). 2. Define a database range for this block (select it, Data->Define Range), and for the newly created range, make sure that "Contains column labels" is checked under Options. 3. Unselect the range; click on any cell inside the range, and click on Sort Ascending or Sort Descending toolbar buttons. The range gest automatically selected including all adjacent non-empty cells; and it is sorted as a whole, including the first row, despite the data range for this selection defined the first row to be labels. 1. When the selected area is the same as some database range, sorting must not sort first row when it's defined as column labels; 2. When the selected area is the same as some database range, sorting must not sort last row when it's defined as totals; 3. When expanding selection, database range in the place of selection should be considered (in case when e.g. database range is smaller/larger than contiguous block).
Code pointers: 1. Database range is available through ScDocument::GetDBAtCursor / ScDocument::GetDBAtArea. 2. Checking if the range has a column header is done in ScTable::HasColHeader (sc/source/core/data/table3.cxx). 3. Expansion of the area happens in ScTable::GetDataArea (sc/source/core/data/table1.cxx).
The effect indicated by Mikе occurs after reopening the file in the case when the entire database range is immediately selected.
(In reply to Mike Kaganski from comment #0) > 1. In a spreadsheet, create a rectangular data block with all cells having > only text (no numbers). > 2. Define a database range for this block (select it, Data->Define Range), > and for the newly created range, make sure that "Contains column labels" is > checked under Options. > 3. Unselect the range; click on any cell inside the range, and click on Sort > Ascending or Sort Descending toolbar buttons. > > The range gest automatically selected including all adjacent non-empty > cells; and it is sorted as a whole, including the first row, despite the > data range for this selection defined the first row to be labels. > > 1. When the selected area is the same as some database range, sorting must > not sort first row when it's defined as column labels; > 2. When the selected area is the same as some database range, sorting must > not sort last row when it's defined as totals; > 3. When expanding selection, database range in the place of selection should > be considered (in case when e.g. database range is smaller/larger than > contiguous block). Forgive me if this is a stupid question. Clarification is needed for point 3 i.e expanding selection As I understand from this blog https://help.libreoffice.org/6.2/en-US/text/scalc/guide/database_sort.html?&DbPAR=WRITER&System=UNIX i.e If you select a range of cells, only these cells will get sorted then why is point 3 saying that database range in the place of selection should be considered.
(In reply to Zain Iftikhar from comment #3) > Clarification is needed for point 3 i.e expanding selection > As I understand from this blog > https://help.libreoffice.org/6.2/en-US/text/scalc/guide/database_sort. > html?&DbPAR=WRITER&System=UNIX > i.e > If you select a range of cells, only these cells will get sorted then why is > point 3 saying that database range in the place of selection should be > considered. Does this work for sort ascending/descending toolbar buttons as expected - especially in a scenario when the range is not equal to adjacent range?
(In reply to Mike Kaganski from comment #4) > (In reply to Zain Iftikhar from comment #3) > > Clarification is needed for point 3 i.e expanding selection > > As I understand from this blog > > https://help.libreoffice.org/6.2/en-US/text/scalc/guide/database_sort. > > html?&DbPAR=WRITER&System=UNIX > > i.e > > If you select a range of cells, only these cells will get sorted then why is > > point 3 saying that database range in the place of selection should be > > considered. > > Does this work for sort ascending/descending toolbar buttons as expected - > especially in a scenario when the range is not equal to adjacent range? I have reproduced a behavior I have defined a Database range and there are some adjacent non-empty cells outside the database range. Now if I select cells inside the database range and try to sort them there will be a popover stating "Sort Range"(with three options "Extend Selection", "Current Selection" and "Cancel") and If I choose Extend Selection now instead of sorting just Database range it also considers adjacent non-empty cells outside the database range. Now the question is "Behavior should be sorting just the Database range when selecting Extend Selection option Or something else" Pardon me If I'm not understanding point 3 properly?
(In reply to Zain Iftikhar from comment #5) Please don't quote everything - this clutters the discussion. Only quote what is immediately relevant to your phrase. Often it's enough to simply answer without quoting, or to use "(In reply to ... from comment ...)" string, or just one sentence that you answer to. > Now the question is "Behavior should be sorting just the Database range when > selecting Extend Selection option Or something else" Pardon me If I'm not > understanding point 3 properly? First of all, I likely needed to make #3 a separate issue. So please disregard #3 for the sake of this issue.
Suggestion: Get sort range 1) Test if the sort range is a named database range (menu:Data>Define). 2a) If so, expand a single cell selection to that range. 2b)) If not, expand a single cell selection to current region. In case of a range selection that is not identical with the db-range nor with the current region, prompt "The cells next to the selection contain data. do your really want ..." or respectively "The selected range intersects with a defined database range. do you really want ...") Separate header row 3a) If we deal with a defined database range, obey the configuration declared for that range. 3b) If not, treat any first row with consecutive string values as a header row. Side note: There is a relatively new option "Contains totals row" only for DatabaseRange. It is not defined for a sort descriptor. 3) Get sort columns Expand selection to what has been found in steps 1) and 2). 3a) If called from Data>Sort command: Show sort dialog with what has been found in step 3). 3b) If called from quick sort button: Sort by the column where the active cell is.
As a temporary work-around, I uploaded https://extensions.libreoffice.org/en/extensions/show/99205 which seems to work pretty well. It detects any database range at the active cell, takes over most settings from that database range and its sort descriptor. It sorts the database range ascending or descending by the field at the active cell. It disregards options "copy sort results" and "sort columns". It just sorts a database-like table in place by the active cell's field analog to the built-in quick sort buttons ".uno:SortAscending" and ".uno:SortDescending, which are dispatched when there is no database range at the active cell.