Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming this is somekind of homework and goal for this is to sort more data than you can hold in your RAM?</p> <p>Since you only have numbers 1-10, this is not that complicated task. Just open your input file and count how many occourances of every specific number you have. After that you can construct simple loop and write values into another file. Following example is pretty self explainatory.</p> <pre><code>$inFile = '/path/to/input/file'; $outFile = '/path/to/output/file'; $input = fopen($inFile, 'r'); if ($input === false) { throw new Exception('Unable to open: ' . $inFile); } //$map will be array with size of 10, filled with 0-s $map = array_fill(1, 10, 0); //Read file line by line and count how many of each specific number you have while (!feof($input)) { $int = (int) fgets($input); $map[$int]++; } fclose($input); $output = fopen($outFile, 'w'); if ($output === false) { throw new Exception('Unable to open: ' . $outFile); } /* * Reverse array if you need to change direction between * ascending and descending order */ //$map = array_reverse($map); //Write values into your output file foreach ($map AS $number =&gt; $count) { $string = ((string) $number) . PHP_EOL; for ($i = 0; $i &lt; $count; $i++) { fwrite($output, $string); } } fclose($output); </code></pre> <p>Taking into account the fact, that you are dealing with huge files, you should also check script execution time limit for your PHP environment, following example will take VERY long for 10GB+ sized files, but since I didn't see any limitations concerning execution time and performance in your question, I'm assuming it is OK.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload