views:

4234

answers:

6

Some amendment of the data in txt file. I have tried the suggested code but Im not successfully write it again in the txt file with this format.I've tried the collection.sort but it write the data in long line.

My txt file contain these data:

Monday
Jessica  Run      20mins
Alba     Walk     20mins
Amy      Jogging  40mins
Bobby    Run      10mins
Tuesday
Mess     Run      20mins
Alba     Walk     20mins
Christy  Jogging  40mins
Bobby    Run      10mins

How can I sort those data in ascending order and store it again in txt file after sorting?

Monday
Alba     Walk     20mins
Amy      Jogging  40mins
Bobby    Run      10mins
Jessica  Run      20mins
Tuesday
Alba     Walk     20mins
Bobby    Run      10 mins
Christy  Jogging  40mins
Mess     Run      20mins
Jessica  Run      20mins
A: 

Using sort:

aaron@ares ~$ sort data.txt 
Alba     Walk     20mins
Amy      Jogging  40mins
Bobby    Run      10mins
Jessica  Run      20mins

aaron@ares ~$ sort data.txt > sorted.txt
aaron@ares ~$ cat sorted.txt
Alba     Walk     20mins
Amy      Jogging  40mins
Bobby    Run      10mins
Jessica  Run      20mins

... or using python:

aaron@ares ~$ python -c "import sys; print ''.join(sorted(sys.stdin))" < data.txt > sorted.txt
aaron@ares ~$ cat sorted.txt 
Alba     Walk     20mins
Amy      Jogging  40mins
Bobby    Run      10mins
Jessica  Run      20mins
Aaron Maenpaa
bah... it wasn't in the question... I hate when people do that.
Aaron Maenpaa
hmm, Runtime.exec()? *ducks*
Joey
+1; it might not exactly answer the question, but if you're only doing a one-off, using `sort` saves writing a utility application.
Rob
nah you missed the monday, tuesday thing ;)
Sujoy
@Sujoy He changed the question after I posted the answer.
Aaron Maenpaa
+3  A: 

Have a class containing 3 parameters : Name , Action , and length.

Open your file for reading , read word by word , for each person create a new instance of the class and fill the appropiate value then insert that instance to some collection.

Sort your collection either by writing you own sort method or (The better option) use one of the Java sorting functions.

Open another file for writing and write your sorted collection in the same fasion.

if you can't translate the above actions into code , Buy the book "Core JAVA" and read it well Or hope someone here will be generous and give you a complete code. :)

Typo in the 3rd paragraph (buy -> by). You (and everyone else - was this an addition through edit?) have ignored the days of the week.
Tom
As far as I remember , The days of the week wasn't there in the pre-edited post .
A: 

If there isn't horrible amount of data in that file, try something like this:

load all of its contents into a

String s = <<file contents>>;

String[] strings = s.split(<<here comes lovely regex something like \w+\s\w+\s\w>>);

Arrays.sort(strings);

for (String str : strings) {
  write  str  to your output file;
}

but i am much too tired to propose something more sensible....

Peter Perháč
+6  A: 

Here's something I came up with:

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

public class Sort {

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader("fileToRead"));
        Map<String, String> map=new TreeMap<String, String>();
        String line="";
        while((line=reader.readLine())!=null){
         map.put(getField(line),line);
        }
        reader.close();
        FileWriter writer = new FileWriter("fileToWrite");
        for(String val : map.values()){
         writer.write(val); 
         writer.write('\n');
        }
        writer.close();
    }

    private static String getField(String line) {
     return line.split(" ")[0];//extract value you want to sort on
    }
}
Peter Dolberg
+1 for using a collection with ordering. No need for a sort routine here...
Jon
+1 per above; nice use of a TreeMap
Rob
-1 for not supporting duplicate keys...
John Nilsson
Not supporting duplicate keys. Good point, but if I thought that was a requirement, I'd have written it differently.
Peter Dolberg
after adding the support of duplicate keys the code was very useful for me :)
Markus
A: 

Here's a pretty lazy way of doing it since you are only sorting first word:

    ArrayList<String> rows = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new FileReader("input.txt"));

    String s;
    while((s = reader.readLine())!=null)
     rows.add(s);

    Collections.sort(rows);

    FileWriter writer = new FileWriter("output.txt");
    for(String cur: rows)
     writer.write(cur+"\n");

    reader.close();
    writer.close();
llamaoo7
A: 

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

public class Sort1 {

public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new FileReader("fileToRead.txt"));
    Map<String, String> map=new TreeMap<String, String>();
    String line="";
    while((line=reader.readLine())!=null){
            map.put(getField(line),line);
    }
    reader.close();
    BufferedWriter writer = new BufferedWriter(new FileWriter("fileToWrite1.txt"));
    for(String val : map.values()){
            writer.write(val);      
            writer.newLine();
    }
    writer.close();
}

private static String getField(String line) {
    return line.split(" ")[0];//extract value you want to sort on
}

}

i altered the previou pgm and got the result....
try it, it will help you.....