tags:

views:

116

answers:

3

Hi there,

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

Using php, I'm trying to create a script which will numerically sort a text file (.txt) in ascending order (lowest to highest). Each entry within the text file is on a new line, therefore I'd like the lines to be numerically sorted. If possible, once it has been numerically sorted, I'd like the data to be written into another text file, titled "newtime.txt" within the same directory. Of course, if possible. ;)

The main part I'm struggling with is that the content within the text file isn't static (e.g. contain x number of lines/words etc.) Infact, it is automatically updated with several lines. Therefore, I'd like all the lines to be updated numerically.

The text file follows the structure:

2 aullah1
12 name
7 username

Of course, which is regularly updated with more lines. Will it be possible to numerically sort the lines? Also, I plan on using a Cron Job to repeat the script every 5 minutes. ;)

P.S. What will happen if there are two same numbers? Will it then go onto sorting out the data alphabetically?

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

Thank you.

+4  A: 

You can do this easily by

Reading the file into an array:

$lines = file($path_to_file);

Sort the array:

natsort($lines);

Write the array back to a new file:

file_put_contents($path_to_new_file, implode(PHP_EOL, $lines));
captaintokyo
The last line should be: `file_put_contents($path_to_new_file, implode(PHP_EOL, $lines));`
shamittomar
Thanks, for correcting me
captaintokyo
Hi captaintokyo, thank you for your response. It is extremely appreciated! Your script worked perfectly in all ways but there was a problem, which was pointed out by jasonbar (scroll down). It seems to display 10 lower than 2, since 10 has the digit 1, which is lower than 2. :( Again, I appreciate your response and I'm extremely grateful for your effort. ;)
AUllah1
+4  A: 

Assuming you are working with a reasonable number of lines (thousands, not millions) this should be sufficient. If you have very large files, you may run into memory issues using file():

<?php

$data = file($file_path);
natsort($data);
file_put_contents($new_file_path, implode("\n", $data));

You could replace the single file_put_contents with a looped fwrite() for each element in $data.

natsort() will sort the numerics correctly (as opposed to asort() which will put 10 before 2). For matching numerics, it will sort as you expect, comparing the rest of the string.

natsort(), file()

jasonbar
Hi jasonbar, thank you for your response, I extremely appreciate it as well as your effort. I tried out your coding and it worked more than flawlessly and it's what I'll be using. Again, I can't thank you enough. ;)
AUllah1
+1  A: 
<?php
$lines = file("time.txt");
natsort($lines);
file_put_contents("newtime.txt", implode("\n", $lines));
?>
Mike
Hi Mike, I appreciate your response. You definitely have a skill within php coding and your code works flawlessly too. Again, thank you for your response and I can't thank you enough for your effort. ;)
AUllah1