Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort paragraph numbers

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.

like image 416
Kirill Avatar asked Jan 08 '18 12:01

Kirill


People also ask

How do I sort paragraph numbers in Word?

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.

How do I sort text in numbers?

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.

How do you sort text and mixed numbers?

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.


1 Answers

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.

like image 59
Julius Vainora Avatar answered Oct 12 '22 23:10

Julius Vainora