Index: scripts/titlegen.py
===================================================================
--- scripts/titlegen.py	(revision 26)
+++ scripts/titlegen.py	(revision 42)
@@ -5,4 +5,7 @@
 import copy
 import re
+
+max_sec_number=100
+max_sec_depth=4
 
 def sec_inc(section, lev):
@@ -19,4 +22,19 @@
     s=s[:-1]
     return s
+
+def compare_sec(id1, id2):
+    id1=id1.split('.')
+    c1=0
+    for s in id1:
+        c1=c1*max_sec_number+int(s)
+    for i in range(len(id1), max_sec_depth+1):
+        c1*=max_sec_number
+    id2=id2.split('.')
+    c2=0
+    for s in id2:
+        c2=c2*max_sec_number+int(s)
+    for i in range(len(id2), max_sec_depth+1):
+        c2*=max_sec_number
+    return c1-c2
 
 section = [];
@@ -70,6 +88,6 @@
                              (prev_str,next_str))
             elif gr[3]:
-                secs = [ x for x in toc]
-                secs.sort()
+                secs = [ x for x in toc ]
+                secs.sort(compare_sec)
                 for num in secs:
                     fo.write("%s - \\ref %s\n"%('  '*((len(ind[toc[num]][0]))),
