aboutsummaryrefslogtreecommitdiff
path: root/sort.py
diff options
context:
space:
mode:
authorHolden Rohrer <hdawg7797@yahoo.com>2019-07-30 13:13:05 -0400
committerHolden Rohrer <hdawg7797@yahoo.com>2019-07-30 13:13:05 -0400
commitd55019e9356e13967cba1bf70521f3a5ef23bea9 (patch)
tree8a595fa9f152d873809aa5d1e1321bc5c90aa9c6 /sort.py
parenta8d1c9fc12789bf8e61e8221cb07451896314835 (diff)
alphabetization trial
Diffstat (limited to 'sort.py')
-rw-r--r--sort.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/sort.py b/sort.py
new file mode 100644
index 0000000..00007bc
--- /dev/null
+++ b/sort.py
@@ -0,0 +1,27 @@
+#Intended as an auxiliary program for citations.tex to alphabetize an output file based on the first group.
+
+#Inputs file sort.tex, to sort as read, takes all data, and then reopens as write
+f = open('sort.tex','r')
+dat = f.readlines()
+f.close()
+f = open('sort.tex','w')
+
+def firstgroup(line): #handles nested brackets to return (first group data, other data) tuple.
+ nesting = 0
+ for charind in range(len(line)):
+ char = line[charind]
+ if char == '{':
+ nesting += 1
+ if char == '}':
+ nesting -= 1
+ if nesting == 0:
+ breakchar = charind+1
+ break
+ return (line[1:breakchar], line[breakchar:])
+
+for lineind in range(len(dat)):
+ line = dat[lineind]
+ dat[lineind] = firstgroup(line)
+
+dat.sort(key=lambda text : [ord(char) for char in text[0]])
+f.write(''.join([line[1] for line in dat]))