Bug 161948 - natural sort of data range is not written to file although a suitable attribute exists
Summary: natural sort of data range is not written to file although a suitable attribu...
Status: ASSIGNED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Regina Henschel
URL: https://docs.oasis-open.org/office/Op...
Whiteboard: target:26.2.0
Keywords:
: 167997 (view as bug list)
Depends on:
Blocks: Sorting
  Show dependency treegraph
 
Reported: 2024-07-08 12:39 UTC by Regina Henschel
Modified: 2025-09-14 20:00 UTC (History)
6 users (show)

See Also:
Crash report or crash signature:


Attachments
Material for natural sort test (14.92 KB, application/vnd.oasis.opendocument.spreadsheet)
2024-07-08 12:39 UTC, Regina Henschel
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Regina Henschel 2024-07-08 12:39:29 UTC
Created attachment 195166 [details]
Material for natural sort test

Open attached file.
Data > Select Range > choose 'naturalSort'
Data > Sort... > Tab Options, check 'Enable natural sort' > OK
Data > Select Range > choose 'alphanumericSort'
Data > Sort... > Tab Options, make sure 'Enable natural sort' is not checked > OK
Save file.

Reopen saved file.
Data > Select Range > choose 'naturalSort'
Data > Sort... > Tab Options.
Error: 'Enable natural sort' is not checked.

Open saved file in editor and inspect element <table:database-ranges> in content.xml. You will find two <table:database-range> elements, one for database range 'naturalSort' the other for database range 'alphanumericalSort'.
Compare their child elements <table:sort>. Besides the cell addresses they are identical.

The element for database range 'naturalSort' should have an attribute table:embedded-number-behavior (19.628, part 3, ODF 1.3) with value 'double' or 'integer'. This attribute is needed to specify, that natural sort has to be used. If you add it manually, it is not read. Support for this attribute is missing and thus the setting 'Enable natural sort' is lost on save/load.
Comment 1 m_a_riosv 2024-07-08 19:45:50 UTC
Reproducible
Version: 25.2.0.0.alpha0+ (X86_64) / LibreOffice Community
Build ID: 7a5d0c413f37e02356d0bd1e1da7a9445ee25fe8
CPU threads: 16; OS: Windows 11 X86_64 (10.0 build 22631); UI render: Skia/Raster; VCL: win
Locale: es-ES (es_ES); UI: en-US
Calc: CL threaded

Aoo has no the option.
It fails in LIbreOffice from the start.
LibreOffice 3.3.0 
OOO330m19 (Build:6)
tag libreoffice-3.3.0.4
Comment 2 Regina Henschel 2024-07-31 12:01:53 UTC
The import is in https://opengrok.libreoffice.org/xref/core/sc/source/filter/xml/xmlsorti.cxx. But the ScXMLSortContext has no suitable member yet.

The export is in https://opengrok.libreoffice.org/xref/core/sc/source/filter/xml/XMLExportDatabaseRanges.cxx

The property "case-sensitive" can be used as a kind of template to see what needs to done.

I propose this as easyHack.
Comment 3 Regina Henschel 2024-07-31 12:08:39 UTC
The specification of the ODF attribute table:embedded-number-behavior is in https://docs.oasis-open.org/office/OpenDocument/v1.3/os/part3-schema/OpenDocument-v1.3-os-part3-schema.html#attribute-table_embedded-number-behavior
Comment 5 Regina Henschel 2024-10-30 16:56:33 UTC
The struct ScSortParam has the members bNaturalSort and aCollatorAlgorithm. The latter is e.g. used in German locale for sort algorithm 'Telefonbuch'.

With a German local I see table:algorithm="alphanumeric" or table:algorithm="phonebook" in the ODF file source. As ODF has only this one attribute for the algorithm, we need to encode the bNaturalSort attribute into this string. We could use a ";" (or a "+" or a "|") sign as delimiter. So we could get table:algorithm="alphanumeric;naturalsort" or table:algorithm="alphanumeric" or table:algorithm="naturalsort".

The export is in /core/sc/source/filter/xml/XMLExportDatabaseRanges.cxx

The import would need to separate the tokens and extend ScXMLSortContext for the bNaturalSort property. The import is in core/sc/source/filter/xml/xmlsorti.cxx.

I'm not sure whether we have "Natural Sort" already in the API.
Comment 6 m_a_riosv 2025-08-18 14:19:34 UTC
*** Bug 167997 has been marked as a duplicate of this bug. ***
Comment 7 Regina Henschel 2025-08-20 08:47:14 UTC
(In reply to Regina Henschel from comment #5)
> As ODF has only this one
> attribute for the algorithm, we need to encode the bNaturalSort attribute
> into this string.

There I was wrong. Correction: ODF has the attribute table:embedded-number-behavior attribute of the <table:sort> element to describe a natural sort.
https://docs.oasis-open.org/office/OpenDocument/v1.4/cs01/part3-schema/OpenDocument-v1.4-cs01-part3-schema.html#attribute-table_embedded-number-behavior.
The standard describes in detail, how natural sorting has to be done.
Comment 8 Regina Henschel 2025-08-24 11:57:50 UTC
I'll try to solve it. There will be three steps:
1. Bring natural sort to API. That's were I'm currently working on in https://gerrit.libreoffice.org/c/core/+/190133 
2. Read/write it with ODF.
3. Implement ODF attribute value 'integer'.
Comment 9 Commit Notification 2025-08-26 10:53:11 UTC
Regina Henschel committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/47eb7f4e4dd0ed72679246b462686e153a1c07d0

tdf161948 bring natural sort to API

It will be available in 26.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 10 Commit Notification 2025-09-02 21:32:43 UTC
Regina Henschel committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/da42aee85993637e5aff3c2bf6c948dfe2f7c1b4

tdf161948 read & write ODF 'embedded-number-behavior'

It will be available in 26.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 11 Regina Henschel 2025-09-14 18:30:58 UTC
This is not set to "Fixed" because I'm still working to bring the "integer" sorting type, that is provided by ODF, to LibreOffice.