# Sorting LaTeX references

I don’t use .bib files and just populate bibliographies like:


\begin{thebibliography}{99}

\bibitem{Romanelli1989}
F.~Romanelli, Phys. Fluids B \textbf{1}, 1018 (1989).

%.... (other citations)



One issue is that this can be out of order compared to the order of /cite{} in the text. The following python script goes through the citations in the text and prints the appropriately ordered bibliography. Replacing the bibliography must be done by hand, but it’s safer that way.


f=open("article.tex","rb")

cites=&#91;]
A=f.tell()

while(True):
a=line.find(r"\cite{")
if(a==-1):
break
else:
line=line&#91;a+5:]
b=line.find("}")
blah=line&#91;1:b]
if(blah!=""):
B=blah.split(",")
for e in B:
if e not in cites:
cites.append(e)
line=line&#91;b:]

refs=&#91;]

f.seek(A)
last_line=""
if(last_line&#91;:5]==r"\bibi"):
refs.append((last_line,line))
last_line=line

for i in cites:
for j in refs:
end=j&#91;0].find("}")
if(i==j&#91;0]&#91;9:end]):
print j&#91;0]&#91;:-1]
print j&#91;1]
continue



~-~-~-~-~-~-~-~