Sort list in order by numeric suffix

Discuss the word processor
Post Reply
sainty88
Posts: 6
Joined: Fri Nov 11, 2022 3:06 pm

Sort list in order by numeric suffix

Post by sainty88 »

Hi,

There must be an easy way to sort data numerically in Writer, but I cant seem to figure it out? By default my list is,

Name 1
Name 10
Name 100
Name 1000
Name 1001
Name 1002
Name 1003
etc etc

What I need is,

Name 1
Name 2
Name 3
Name 4
etc etc

Could someone tell me how to do this please?

Thanks
OpenOffice 4.1.12
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Sort list in numerical order?

Post by Zizi64 »

try to create a helper column with numbers converted from the texts.

Then sort the data based on the converted numbers.

example:
https://forum.openoffice.org/en/forum/v ... hp?t=53379
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Sort list in numerical order?

Post by JeJe »

Are you trying to repace 10 with a 2 in the second name and the 100 with a 3 in the third name etc.?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bill
Volunteer
Posts: 8934
Joined: Sat Nov 24, 2007 6:48 am

Re: Sort list in numerical order?

Post by Bill »

If the list is in a table with the names in one column and the numbers in another column, select the table then use Tools > Sort to sort the table using the numbers column as the sort key. If the list is not in a table, it must be converted to a table before it can be sorted.

If you need detailed instructions, upload a sample file.

After a closer look, the list doesn't have to be converted to a table, but there needs to be a separator character between the names and the numbers. A tab is usually used for the separator character.
AOO 4.1.14 on Ubuntu MATE 22.04
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Sort list in numerical order?

Post by JeJe »

Bill wrote: Thu Nov 24, 2022 9:42 pm After a closer look, the list doesn't have to be converted to a table, but there needs to be a separator character between the names and the numbers. A tab is usually used for the separator character.
Ifs confusing for me because the first list is already in sort order. A tab can be added in front of the numbers with the following search and replace terms in the find/replace dialog and regular expressions ticked

\d*

\t&

(Explanation:
\d means any digit, * means any number of the preceding character

\t means a tab, & means include what was there originally)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Hagar Delest
Moderator
Posts: 32666
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Sort list in numerical order?

Post by Hagar Delest »

Here is what worked for me, with a space in the Character field (in LibreOffice):
Sort.png
Sort.png (48.14 KiB) Viewed 749 times
that gives (for example):
Name 1
Name 2
Name 10
Name 23
Name 100
Name 1000

Alphanumeric order gives Name 1000 before Name 2 since the first different character is taken into account (1).
Thus, you have to specify that you need a numeric order.

Please add [Solved] at the beginning of the title in your first post (top of the topic) with the 🖉 button if your issue has been fixed.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Bill
Volunteer
Posts: 8934
Joined: Sat Nov 24, 2007 6:48 am

Re: Sort list in numerical order?

Post by Bill »

Using space as the Separator works on the example list, but I doubt that the real list just has "Name" on every line in the list. If there are real names like "John Smith" instead of just "Name", then there will be spaces in the names and a different character would have to be used for the Separator. A sample document with a real list is needed.
AOO 4.1.14 on Ubuntu MATE 22.04
User avatar
Hagar Delest
Moderator
Posts: 32666
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Sort list in numerical order?

Post by Hagar Delest »

Indeed. But the idea is there. If there is an issue with its implementation sainty88, feel free to post again with a sample file as suggested by Bill.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Sort list in numerical order?

Post by karolus »

Simple approch to »natural-sort« selected paragraphs :

Code: Select all

import re

num_rex = re.compile(r"(\D+|\d+)")

def number_keys( para ):
    match = num_rex.findall(para)
    return [
        int(group) if group.isdigit()
        else group for group in match
            ]

def natural_sort_paras(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    paras = doc.CurrentSelection[0]
    paras.String = '\r'.join(sorted(paras.String.split('\n'),
                                    key=number_keys))
    
run natural_sort_paras after selecting the Paras to sort.

for organizing your python-stuff use: apso.oxt from here
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Sort list in numerical order?

Post by JeJe »

karolus wrote: Fri Nov 25, 2022 1:08 pm Simple approch to »natural-sort« selected paragraphs :
It looks like formatting would be lost if there is any. From the OP's other thread, they have 5000 items, so might that run into the string size limit?

Another option might be to cut and paste or "paste special" into calc and sort it there and paste back.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Sort list in numerical order?

Post by karolus »

JeJe wrote: Fri Nov 25, 2022 2:48 pm
karolus wrote: Fri Nov 25, 2022 1:08 pm Simple approch to »natural-sort« selected paragraphs :
It looks like formatting would be lost if there is any. From the OP's other thread, they have 5000 items, so might that run into the string size limit?
Assuming each paragraph has less than 100 chararcters, it should not a problem for python.
If each paragraph consists on 64kbit it would be a problem… but does it make sense to sort 5000 paragraphs each with 64kb ???
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply