Index: trunk/src/tmcsim/cadsimulator/Coordinator.java
===================================================================
--- trunk/src/tmcsim/cadsimulator/Coordinator.java	(revision 448)
+++ trunk/src/tmcsim/cadsimulator/Coordinator.java	(revision 465)
@@ -8,6 +8,9 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Observer;
 import java.util.TreeMap;
@@ -744,8 +747,33 @@
             Logger.getLogger(Coordinator.class.getName()).log(Level.SEVERE, null, ex);
         }
-        // Initialize an output buffer
-        StringBuffer output = new StringBuffer();
+        // Write output list to a file
+        try 
+        {
+            PrintWriter writer = new PrintWriter(new FileWriter(kCADcommentLog));
+            // process the list of comments obtained from helper method
+            for (String item: getSortedComments(incList))
+            {
+                writer.print(item.substring(9));
+            }
+            writer.close();
+        } catch (Exception exc) 
+        {
+            exc.printStackTrace();
+        } 
+    }
+    
+    /** Extract a list of comments from the current incidents, ordered
+     * by timestamp.  Fixes defect #175.
+     * Package private to allow unit testing.
+     * 
+     * @param incidentList the incidents from which comments are desired
+     * @return list of comments, ready to be output.
+     */
+    List<String> getSortedComments(Vector<Incident> incidentList)
+    {
+        // Initialize a list of for outputing CAD comments
+        ArrayList<String> commentList = new ArrayList<String>();
         // Process all the incidents
-        for (Incident incident: incList)
+        for (Incident incident: incidentList)
         {
             // Retrieve the table of comments/notes the users created
@@ -755,28 +783,38 @@
             {
                 // Combine the fields into one output entry
+                String timestamp = (String)notesTable.getValueAt(row,1);
                 String initials = (String) notesTable.getValueAt(row,2); // user initials
                 // If there are user intials, include this item.
                 // Zero length initials mean it's a scripted item, ignore it.
+                StringBuffer output = new StringBuffer();
                 if (initials.length() > 0)
-                {//CAD Log Entry, Incident #187, Sharon: REQUEST EXTRA ANCHOVIES
-                    output.append("CAD log, ");
+                {//17:03:19 CAD Log Entry, Incident #187, Sharon: REQUEST EXTRA ANCHOVIES
+                    output.append(timestamp);
+                    output.append(" CAD log, ");
                     output.append("Incident #" + incident.logNum + ", ");
                     output.append(initials + ": ");
                     output.append(notesTable.getValueAt(row,4) + "\n"); // notes
+                    commentList.add(output.toString()); // Add this comment to the list
                 }
             }
         }
-        // Write output buffer to a file
-        try 
-        {
-            PrintWriter writer = new PrintWriter(new FileWriter(kCADcommentLog));
-            writer.print(output);
-            writer.close();
-        } catch (Exception exc) 
-        {
-            exc.printStackTrace();
-        } 
-    }
-    
+        // Order the comments by timestamp instead of incident number.
+        Collections.sort(commentList, new java.util.Comparator<String>() 
+        {
+            // provide a comparator that can compare number strings
+            public int compare(String o1, String o2) 
+            {
+                return extractInt(o1) - extractInt(o2);
+            }
+            // extract an integer from a String    
+            int extractInt(String s) 
+            {   // remove all non-digits
+                String num = s.replaceAll("\\D", "");
+                // return 0 if no digits found, else return the number
+                return num.isEmpty() ? 0 : Integer.parseInt(num);
+            }
+        });
+        return commentList;
+    }
     /** Write the currentSimTime to a file. 
        This will be read asynchronously by web clients, e.g.,
