Jump to content

Numerically sorting lists


Schlo_50

Recommended Posts

Hello,

 

I have a short list of numbers in a file and Im trying to find the highest number from it.

 

The list looks like this: (short example)

 

none|14

none|103

none|65

 

I'd like to, if possible, find the highest number of bkey[1] and print it out on the page. I have included my code so far below. At the moment is prints nothing but 1's.

 

print "The largest number in the file is:<br /><br />";

$bfile = file("numbers.DAT");

  foreach($bfile as $bKey => $Val){
   $Data[$bKey] = explode("|", $Val);
   $none = $Data[$bKey][0];
   $number = $Data[$bKey][1];
   
$arr = array($number);
print ksort($arr);
print "<br />";

  }

 

Thanks in advance

Link to comment
Share on other sites

I have kept on trying and have come up with the following but the problem is that It only displays the last number of the list which isn't necessarily the largest number..

 

print "The largest number in the file is:<br /><br />";

$bfile = file("numbers.DAT");

  foreach($bfile as $bKey => $Val){
   $Data[$bKey] = explode("|", $Val);
   $none = $Data[$bKey][0];
   $number = $Data[$bKey][1];
   
$arr = array($number);
  }
print "Largest: ".max($arr);

Link to comment
Share on other sites

Thanks for the help. I tried that code and it didn't quite work after testing, although it's nearly there. I'll show you the input and output.

 

//list of numbers
none | 1 
none | 2 
none | 13 
none | 4 
none | 5 
none | 6 
none | 1010 
none | 950 
none | 93 
none | 8 
none | 11 
none | 12 
none | 3 
none | 14 
none | 16

 

//output
950

 

The output should infact be 1010 in this case

Link to comment
Share on other sites

Very odd, I tested it using an array and it worked fine. You may be on a different PHP version to me, so I have made two changes to the core code - give them a go.

 

<?php

print "The largest number in the file is:<br /><br />";

$bfile = array(
'none | 1',  
'none | 2', 
'none | 13',  
'none | 4', 
'none | 5',  
'none | 6', 
'none | 1010',  
'none | 950',  
'none | 93',  
'none | 8', 
'none | 11',  
'none | 12', 
'none | 3', 
'none | 14', 
'none | 16'
);

foreach($bfile as $key => $value)
{
$tmp = explode('|', $value);
$vals[] = trim($tmp[1]);
}

rsort($vals, SORT_NUMERIC);

echo $vals[0].'<br />';

Link to comment
Share on other sites

Following is not working either:

 

function BubbleSort($sort_array,$column = 0,$reverse=0)
{
  $lunghezza=count($sort_array);
  for ($i = 0; $i < $lunghezza ; $i++){
    for ($j = $i + 1; $j < $lunghezza ; $j++){
      if($reverse){
        if ($sort_array[$i][$column] < $sort_array[$j][$column]){
          $tmp = $sort_array[$i];
          $sort_array[$i] = $sort_array[$j];
          $sort_array[$j] = $tmp;
        }
      }else{
        if ($sort_array[$i][$column] > $sort_array[$j][$column]){
          $tmp = $sort_array[$i];
          $sort_array[$i] = $sort_array[$j];
          $sort_array[$j] = $tmp;
        }
      }
    }
  }
  return $sort_array;          
}


$bfile = file("numbers.DAT");

foreach($bfile as $key => $value)
{
$tmp = explode('|', $value);
$vals[] = $tmp[1];
}

$vals = BubbleSort($vals);

print $vals[1].'<br />';


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.