Sort the unsorted text file and rewrite to same text file in sorted order
<p>I have a question.  I am in process of learning how to read/write files, but having little trouble trying to do both at the same time in same php script.  I have a text file with words like this,</p>

<pre><code>Richmond,Virginia
Seattle,Washington
Los Angeles,California
Dallas,Texas
Jacksonville,Florida
</code></pre>

<p>I wrote a code to sort them in order and this will display in sort order by City.</p>

<pre><code>&lt;?php
$file = file("states.txt");
sort($file);
for($i=0; $i&lt;count($file); $i++)
{
  $states = explode(",", $file[$i]);
  echo $states[0], $states[1],"&lt;br /&gt;";
}
?&gt;
</code></pre>

<p>From this, how can I rewrite those sorted information back into the states.txt file?</p>
