I have a simple table with paragraph numeration:
> table <- data.frame(id=c(1,2,3,4,5,6,7,8,9), paragraph=c("1.1.1.1","1","2","1.1","100","1.2","10","1.1.1","1.1.2"))
> print(table)
id paragraph
1 1.1.1.1
2 1
3 2
4 1.1
5 100
6 1.2
7 10
8 1.1.1
9 1.1.2
10 1.10
I would like to sort it by this way:
id paragraph
2 1
4 1.1
8 1.1.1
1 1.1.1.1
9 1.1.2
6 1.2
10 1.10
3 2
7 10
5 100
The issue for me (I could probably split them by .
to the data.frame
and then apply multiple column ordering), is that I don't know how many dots could be in the output – the amount could vary from time to time.
You can sort a one-level bulleted or numbered list so the text appears in ascending (A to Z) or descending (Z to A) alphabetical order. Select the list you want to sort. Go to Home > Sort. Set Sort by to Paragraphs and Text.
In the Sort dialog box, under Column, in the Sort by box, select the first column that you want to sort. Under Sort On, select the type of sort. Do one of the following: To sort by text, number, or date and time, select Values.
Creating Sort Columns Select the column to the right of the mixed cell you want to sort (Employee Number in this example). Right-click the column labels and click Insert to add a column. Press CTRL-Y to repeat the action and add another column.
Here's one option:
sp <- strsplit(as.character(table$paragraph), "\\.")
ro <- sapply(sp, function(x) sum(as.numeric(x) * 100^(max(lengths(sp)) + 0:(1 - length(x)))))
table[order(ro), ]
# id paragraph
# 2 2 1
# 4 4 1.1
# 8 8 1.1.1
# 1 1 1.1.1.1
# 9 9 1.1.2
# 6 6 1.2
# 10 10 1.10
# 3 3 2
# 7 7 10
# 5 5 100
As, clearly, the levels structure cannot be ignored, with sp
I first split the paragraph numbers. Then, as to translate paragraph numbers into integers by preserving the order, for each paragraph number I multiply the number of the section by 100^n (for a particular n), of the subsection by 100^(n-1), and so on (using 100 should suffice in practice but you could also use larger numbers), so that their sum is the desired integer, and ro
is a vector of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With