Writer not sort following ascii code

Libreoffice does not order as one might think based on the ascii code of the individual characters, but rather on the collating of the algorithm linked to the chosen language
In calc the problem is solved by inserting custom sorting criteria in Tools/Options/Calc/Sort Lists

In writer there doesn’t seem to be this option, so sorting the following sequence of characters, already sorted by ascii code

!
#
$
%
&
(
)
*
+
,
-
.
/
0

You get:

-
,
!
.
(
)
*
/
&
#
%
+
$
0

Is there a way to solve this problem, which has been present since the first versions of writer ?
Thanks for every contribution

Writer is not an API, and the encoding of the characters is something completely unrelated to its intended use (which is to be used to express stuff in natural language, and thus sorting according to the natural language rules → collation).

So IMO, sorting by ASCII is completely unrelated to what Writer is designed for. Dictionary sorting (according to collation rules), and “natural” sorting (again according to these rules, but taking multi-digit number values into account), to the contrary, are relevant.

Check if setting language to none helps, though (I can’t test ATM).

2 Likes

Thanks for the reply. I tried both in the language options and in the language combobox in the sorting tab of writer to set “none”, without getting any result.
I’ve tried quite a few different languages ​​as well but it seems all of them, regarding the characters I’ve listed, move differently.
Perhaps the problem can be circumvented in other ways. For example using doing the sort with a macro or using calc as support by copying the text to sort
But in both cases, unless create only an index to be used to access an unordered list of paragraphs in an ordered way, it is still a complication that I can’t afford right now.
Thanks again :slightly_smiling_face:

Perhaps use another text editor to pre-sort chunks of text-lines? (Notepad++ maybe?)

Use another editor, but then I might as well use calc, it’s useful for text that I won’t need to edit in the future, much less for ever-changing text that requires frequent sorting
Copying and pasting text into the editor would force me to reset all styles by hand, it’s impractical.
Thanks :slightly_smiling_face:

python is your friend:

2 Likes

Unfortunately, I wrote this in the past, I only use starbasic.
I know javascript very well, but I always had problems interfacing LO with javascript (language supported by LO), too complicated, for now
I’m investing a lot, and profitably, with starbasic, deepening its also poorly documented aspects, for now I don’t want to waste time on anything else. I recently turned 65, at my age time is a precious resource, my priority, for now, is starbasic and all the accessible features provided by LO.
When I can I will try to integrate with other languages ​​(Python, javascript) what starbasic doesn’t give me at the moment, realizing only support functions.
In that case your solution will be very helpful to me, thank you :slightly_smiling_face:

Does that “ScriptForge” thing provide what you need maybe? I don’t know if its text sort is collation-based, (likely it should) but you may create arrays of Longs filled with Asc function, and then the sorted result converted back using Chr.

2 Likes

True, I hadn’t thought of scriptforge
Up until now I have used it little, I suspect it is slower than the equivalent starbasic, but in certain areas it has introduced some notable improvements for those who work in starbasic
I had read something like this about collation-based support in scriptforge, maybe even at the document level.
I’ll check and then I’ll tell you.
However, the problem of ordering directly on the document (paragraphs) remains, avoiding the transposition on the arrays with subsequent restoration in the document to avoid the loss of the formatting which would then have to be restored

I can only read code, and I’m sure it wouldn’t ever outperform Python, let alone native code … but possibly, given that Sort calls SF_Array._HeapSort, which calls SF_Array._HeapSort1, which calls SF_Array._ValCompare, which calls SF_Utils._VarTypeExt and (for strings) StrComp, it must be efficient :stuck_out_tongue_winking_eye: (it uses quicksort though, and operates on indices, so performance was definitely considered when it was written).

1 Like

I begin by report that the scriptforge sort works perfectly, respecting the ascii sequence (thank Mike)
This is the code:

function sort()
	dim a,b,s
	GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
	b=Array(",","!",".","(",")","*","/","&","#","%","+","$","0")  ' b = not sorted
	a = SF_Array.Sort(b, CaseSensitive := True)                   ' a= sorted
 
    s="not sorted ->" + join(b,"") + chr(13)+chr(10) + "now  sorted ->" +  join(a,"")
    msgbox s
' ",","!",".","(",")","*","/","&","#","%","+","$","0"
end function

Using the method sort of scriptforge.array it is a good starting point that solves part of the problem for me.

At worst I can read the textparagraph.string to be sorted with a macro, sort them in an array, and always build via macro an ordered sequence of hyperlinks to the paragraphs, without affecting the paragraphs to be sorted.

If I find something better (sorting on document.text), I’ll let you know.

Thanks again for everyone’s contributions :+1:

1 Like

I may have found the correct solution, I had already successfully used it to sort the headings of a given hierarchical level of a document based on each one’s “String” property.

  1. Select or construct a textCursor on the block of paragraphs to sort
  2. Scan the paragraphs using the createEnumeration method on the selection or on the textCursor saving in an aParRef array (or an enumeration map) the objectReferences to the paragraphs, one per item
  3. Load an aStringToSort array with the “String” properties of each textparagraph by adding a final suffix at the end of each string, preceded by a unique sequence of characters (e.g. «~§§~» followed by a progressive index (0,1, 2,3,4 etc) possibly formatted with initial ‘0’ to have a textual index of fixed length)
  4. Sort the array aStringToSort
  5. Scan aStringToSort from the beginning, extracting from each item the index Idx at the end of the string.
  6. For each item use idx to access in aParRef the object reference of each paragraph to move it (or perform a cut and paste) in the correct sequence

This solution should allow you to sort and move the paragraphs to the correct position without having to touch and reset the formatting

As soon as I can I will also make the code.

Thanks again everyone for all the suggestions

Edit 23, november: In the attached file isI included the code that implements the solution reported in recent days.
The delay in implementation is due to problems due to my lack of knowledge in using cursors, especially in table cells, partly solved by making this code.
The instructions for use are contained within and in the comments of the functions.
Clearly the sort of strings of scriptforge does not the natural order, as it is not foreseen by the standard sorting of writer
This sorting preserves direct and styled formatting

For any questions or bug reports let me know

askLO_2022-11-23_sortOfParagraph.odt (23.3 KB)

1 Like