Q:

Java - Comparing Strings which are numbers

How to compare String which are numbers?
I have Strings saved in file:
"2000 12 7 0 2 -3.0"
"2000 7 7 0 2 -4.0"
"2013 7 23 20 59 25.5"

First five values are date (year, month, day, hour, minute). I need to sort them chronologically.
Here is my code:
 class Task3 {

    BufferedWriter writer;
    BufferedReader reader;
    int licznik = 0;

    Task3() throws IOException {
        this.writer = new BufferedWriter(new FileWriter("c:\\zadanie.txt"));
        this.reader = new BufferedReader(new FileReader("c:\\zadanie.txt"));

    }

    public void WriteMeasur(Measur measur) throws IOException{

       //licznik++;
        writer.write(measur.toString());
        writer.newLine();
    }

    public void WriteChrono(Measur measur) throws IOException{
        List<String> lista = new ArrayList<>();

        String line;        
        while((line = reader.readLine()) != null){
            lista.add(line);            
        }
    }
}

class Time implements Serializable{
    int year, month, day, hour, minute;

    Time(int r, int m, int d, int h, int min){
        if(r <= 2014 && r > 1990)
            this.year = r;
        if(m <= 12)
            this.month = m;
        if(d <= 30 || (m == 2 && d < 29))
            this.day = d;
        if(h <= 24)
            this.hour = h;
        if(min <= 60)
            this.minute = min;
    }

    @Override
    public String toString(){
        return String.valueOf(year) + " " + String.valueOf(month) +  " " +String.valueOf(day) + " " +String.valueOf(hour) +  " " +String.valueOf(minute);

    }
}
class Measur implements Serializable{
    Time time;
    double temp;

    Measur(Time czas, double temp){
        this.time = czas;
        this.temp = temp;

    }

    @Override
    public String toString(){
        return time.toString() + " " +String.valueOf(temp);

    }

}

public class Main{
    public static void main(String args[]) throws IOException{

        //Argumenty klasy czas: rok, miesiac, dzien, godzina, minuty
        // Metody inicjujace klase czas

        Time czas_1 = new Time(2013,7,23,20,59);
        Time czas_3 = new Time(2000,07,7,25,2);
        Time czas_2 = new Time(2000,12,7,25,2);

        // Metody inicjujace klase pomiar

        Measur pomiar_1 = new Measur(czas_1, 25.5);
        Measur pomiar_2 = new Measur(czas_2, -3);
        Measur pomiar_3 = new Measur(czas_3, -4);

        Task3 zad3 = new Task3();

        zad3.WriteMeasur(pomiar_1);
        zad3.WriteMeasur(pomiar_2);
        zad3.WriteMeasur(pomiar_3);
        zad3.writer.close();

    }
}

I have no idea how to sort it. I wanted use Collections.sort(), but in Strings 2000 12 is smaller then 2000 7.
Any advice? 
Thanks!

A:

I'm not sure this is a good idea or not.  But you could use Collections.sort support of Comparator to customise the way in which values are compared while they are been sorted and SimpleDateFormat to convert the value to a Date value which can then be compared directly...for example...
List<String> values = new ArrayList<String>(25);
values.add("2013 7 23 20 59 25.5");
values.add("2000 12 7 0 2 -3.0");
values.add("2000 7 7 0 2 -4.0");

Collections.sort(values, new Comparator<String>() {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd");
    @Override
    public int compare(String o1, String o2) {
        int result = -1;
        try {
            Date d1 = sdf.parse(o1);
            Date d2 = sdf.parse(o2);
            result = d1.compareTo(d2);
        } catch (ParseException exp) {
            exp.printStackTrace();
        }
        return result;
    }
});

for (String value : values) {
    System.out.println(value);
}

