import java.io.*;
import java.util.*;

class LineSort {

    public static void main(String[] args) {

	Vector lines = new Vector();
	    // (With no idea how many lines will be read in, letting the
	    // vector capacity double for each expansion makes sense.)

	try {
	    InputStream in;
	    if (args.length < 1)
		in = System.in;
	    else
		in = new FileInputStream(args[0]);

	    LineInputStream lineIn =	// from Exercise 11.2
		new LineInputStream(in);

	    try {
	      reading:		// label to continue from nested loop
		while (true) {
		    String line = lineIn.readLine();

		    // from beginning, search for element this line is
		    // "less than", and insert before it...
		    for (int i = 0; i < lines.size(); i++) {
			if (line.compareTo((String)lines.elementAt(i)) < 0) {
			    lines.insertElementAt(line, i);
			    continue reading;
			}
		    }
		    lines.addElement(line);	// ...or add to end of list

		    // This linear search has O(n^2) performance with respect
		    // to file size; a binary search would scale much better.
		}
	    } catch (EOFException ignore) {
	    }
	} catch (IOException e) {
	    error("I/O Exception: " + e);
	}

	// print out the sorted lines
	for (int i = 0; i < lines.size(); i++)
	    System.out.println(lines.elementAt(i));
    }

    public static void error(String err) {
	System.err.println("LineSort: " + err);
	System.exit(1); // non-zero argument means "not good"
    }
}
