Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort lines on a value in them
    primarykey
    data
    text
    <p>I am a newbie trying to learn Scala after using PHP for many years. Problem I am trying to solve is really simple but not sure how to do in Scala. Basically I am reading content from a file which have person first name, last name and grades. I need to read the file and sort the names by grades. </p> <p>File is like this.</p> <pre><code>Aoe Samore 3.1 Boe Sbmore 2.2 Coe Scmore 3.9 Doe Sdmore 2.4 Eoe Semore 3.5 Foe Sfmore 2.6 Goe Sgmore 3.7 Hoe Shmore 2.9 Ioe Simore 3.1 Joe Sjmore 1.2 Koe Skmore 3.2 Loe Slmore 4.0 </code></pre> <p>End result should be just display like this </p> <pre><code>Loe Slmore 4 Coe Scmore 3.9 Goe Sgmore 3.7 Eoe Semore 3.5 Koe Skmore 3.2 Aoe Samore 3.1 Ioe Simore 3.1 Hoe Shmore 2.9 Foe Sfmore 2.6 Doe Sdmore 2.4 Boe Sbmore 2.2 Joe Sjmore 1.2 </code></pre> <p>This is how I did in PHP</p> <pre><code>$content = file_get_contents('grd.txt'); $lines = explode("\n",$content); $grades_with_information = []; foreach($lines as $line) { $temp = explode(' ',$line); $temp[2] = (float)$temp[2]; $grades_with_information[] = $temp; } usort($grades_with_information, function($a, $b) { if ($a[2] == $b[2]) { return 0; } return ($a[2] &gt; $b[2]) ? -1 : 1; }); foreach($grades_with_information as $grade_with_information){ echo $grade_with_information[0].' ' .$grade_with_information[1].' ' .$grade_with_information[2] .'&lt;br&gt;'; } </code></pre> <p>How we about doing this in Scala so far I have done this</p> <pre><code>val source = scala.io.Source.fromFile("grd.txt") val lines = source.getLines() for (a &lt;- lines) { } source.close() </code></pre> <p>Any suggestion/help/clues? </p>
    singulars
    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. 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