title: Issue with custom script to sort Arrays ascending and descending order
body: <p>I have an issue to deal with here (a logical error in my code 99%). I just can't seem to find the way to fix it, but I bet one of you will find the problem in no time!</p>
  
  <p>I have to create a function which sorts array passed to it in asc or desc order, but can't use any array sorting functions !</p>
  
  <p>I've been struggling with loops until now and I finally want to ask help from other devs ( you ).</p>
  
  <p>Currently only code for ascending is worked on, descending will be no problem I assume once I do this one. It kinda of does sort values up to some point, but then stops ( it stops if the next smallest value is at the end of the passed array ). What could I do to prevent this and make it sort the whole array and it's elements? </p>
  
  <p>Here is the code so far.</p>
  
  <pre><code>&lt;?php
  
  function order_array($array,$mode = 'ascending') {
  
      $length = count($array);
  
      if($mode == 'descending') {     
          return $array;
      } else {
  
          $sorted_array = array();
          $used_indexes = array();
  
          for($i = 0; $i &lt; $length; $i++) {
              $smallest = true;
  
              echo $array[$i] . '&lt;br/&gt;';
              for($y = 0; $y &lt; $length; $y++) {
  
                  //echo $array[$i] . ' &gt; ' . $array[$y] . '&lt;br/&gt;';
                  // if at ANY time during checking element vs other ones in his array, he is BIGGER than that element
                  // set smallest to false
                  if(!in_array($y,$used_indexes)) {
                      if($array[$i] &gt; $array[$y]) {
                          $smallest = false;
                          break;
                      }
                  }
              }
  
              if($smallest) {
                  $sorted_array[] = $array[$i];
                  $used_indexes[] = $i;
              }
  
          }
  
          return $sorted_array;
      }
  }
  
  $array_to_sort = array(1, 3, 100, 99, 33, 20);
  $sorted_array = order_array($array_to_sort);
  print_r($sorted_array);
  
  ?&gt;
  </code></pre>