How do I use Java to sort surnames in alphabetical order from file to file?

Posted by user577939 on Stack Overflow See other posts from Stack Overflow or by user577939
Published on 2011-01-17T00:46:13Z Indexed on 2011/01/17 1:53 UTC
Read the original article Hit count: 565

Filed under:

I have written this code and don't know how to sort surnames in alphabetical order from my file to another file.

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

class Asmuo {
    String pavarde;
    String vardas;
    long buvLaikas;
    int atv1;
    int atv2;
    int atv3;
}

class Irasas {
    Asmuo duom;
    Irasas kitas;
}

class Sarasas {
    private Irasas p;

    Sarasas() {
        p = null;
    }

    Irasas itrauktiElementa(String pv, String v, long laikas, int d0, int d1,
            int d2) {
        String pvrd, vrd;
        int data0;
        int data1;
        int data2;
        long lks;
        lks = laikas;
        pvrd = pv;
        vrd = v;
        data0 = d0;

        data1 = d1;

        data2 = d2;
        Irasas r = new Irasas();
        r.duom = new Asmuo();
        uzpildymasDuomenimis(r, pvrd, vrd, lks, d0, d1, d2);
        r.kitas = p;
        p = r;
        return r;
    }

    void uzpildymasDuomenimis(Irasas r, String pv, String v, long laik, int d0,
            int d1, int d2) {
        r.duom.pavarde = pv;
        r.duom.vardas = v;
        r.duom.atv1 = d0;
        r.duom.buvLaikas = laik;
        r.duom.atv2 = d1;
        r.duom.atv3 = d2;
    }

    void spausdinti() {
        Irasas d = p;
        int i = 0;
        try {
            FileWriter fstream = new FileWriter("rez.txt");
            BufferedWriter rez = new BufferedWriter(fstream);
            while (d != null) {
                System.out.println(d.duom.pavarde + " " + d.duom.vardas + " "
                        + d.duom.buvLaikas + " " + d.duom.atv1 + " "
                        + d.duom.atv2 + " " + d.duom.atv3);
                rez.write(d.duom.pavarde + " " + d.duom.vardas + " "
                        + d.duom.buvLaikas + " " + d.duom.atv1 + " "
                        + d.duom.atv2 + " " + d.duom.atv3 + "\n");
                d = d.kitas;
                i++;
            }
            rez.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

    }
}

public class Gyventojai {

    public static void main(String args[]) {
        Sarasas sar = new Sarasas();
        Calendar atv = Calendar.getInstance();
        Calendar isv = Calendar.getInstance();

        try {
            FileInputStream fstream = new FileInputStream("duom.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String eil;
            while ((eil = br.readLine()) != null) {
                String[] cells = eil.split(" ");
                String pvrd = cells[0];
                String vrd = cells[1];
                atv.set(Integer.parseInt(cells[2]), Integer.parseInt(cells[3]),
                        Integer.parseInt(cells[4]));
                isv.set(Integer.parseInt(cells[5]), Integer.parseInt(cells[6]),
                        Integer.parseInt(cells[7]));
                long laik = (isv.getTimeInMillis() - atv.getTimeInMillis())
                        / (24 * 60 * 60 * 1000);
                int d0 = Integer.parseInt(cells[2]);
                int d1 = Integer.parseInt(cells[3]);
                int d2 = Integer.parseInt(cells[4]);
                sar.itrauktiElementa(pvrd, vrd, laik, d0, d1, d2);

            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

        sar.spausdinti();

    }
}

© Stack Overflow or respective owner

Related posts about java