original_string: protected static function sort_records(&$records, $sortfields) {
          if (empty($records)) {
              return;
          }
          // If sorting by course display name, calculate it (it may be fullname or shortname+fullname).
          if (array_key_exists('displayname', $sortfields)) {
              foreach ($records as $key => $record) {
                  if (!isset($record->displayname)) {
                      $records[$key]->displayname = get_course_display_name_for_list($record);
                  }
              }
          }
          // Sorting by one field - use core_collator.
          if (count($sortfields) == 1) {
              $property = key($sortfields);
              if (in_array($property, array('sortorder', 'id', 'visible', 'parent', 'depth'))) {
                  $sortflag = core_collator::SORT_NUMERIC;
              } else if (in_array($property, array('idnumber', 'displayname', 'name', 'shortname', 'fullname'))) {
                  $sortflag = core_collator::SORT_STRING;
              } else {
                  $sortflag = core_collator::SORT_REGULAR;
              }
              core_collator::asort_objects_by_property($records, $property, $sortflag);
              if ($sortfields[$property] < 0) {
                  $records = array_reverse($records, true);
              }
              return;
          }
  
          // Sort by multiple fields - use custom sorting.
          uasort($records, function($a, $b) use ($sortfields) {
              foreach ($sortfields as $field => $mult) {
                  // Nulls first.
                  if (is_null($a->$field) && !is_null($b->$field)) {
                      return -$mult;
                  }
                  if (is_null($b->$field) && !is_null($a->$field)) {
                      return $mult;
                  }
  
                  if (is_string($a->$field) || is_string($b->$field)) {
                      // String fields.
                      if ($cmp = strcoll($a->$field, $b->$field)) {
                          return $mult * $cmp;
                      }
                  } else {
                      // Int fields.
                      if ($a->$field > $b->$field) {
                          return $mult;
                      }
                      if ($a->$field < $b->$field) {
                          return -$mult;
                      }
                  }
              }
              return 0;
          });
      }
code: protected static function sort_records(&$records, $sortfields) {
          if (empty($records)) {
              return;
          }
          // If sorting by course display name, calculate it (it may be fullname or shortname+fullname).
          if (array_key_exists('displayname', $sortfields)) {
              foreach ($records as $key => $record) {
                  if (!isset($record->displayname)) {
                      $records[$key]->displayname = get_course_display_name_for_list($record);
                  }
              }
          }
          // Sorting by one field - use core_collator.
          if (count($sortfields) == 1) {
              $property = key($sortfields);
              if (in_array($property, array('sortorder', 'id', 'visible', 'parent', 'depth'))) {
                  $sortflag = core_collator::SORT_NUMERIC;
              } else if (in_array($property, array('idnumber', 'displayname', 'name', 'shortname', 'fullname'))) {
                  $sortflag = core_collator::SORT_STRING;
              } else {
                  $sortflag = core_collator::SORT_REGULAR;
              }
              core_collator::asort_objects_by_property($records, $property, $sortflag);
              if ($sortfields[$property] < 0) {
                  $records = array_reverse($records, true);
              }
              return;
          }
  
          // Sort by multiple fields - use custom sorting.
          uasort($records, function($a, $b) use ($sortfields) {
              foreach ($sortfields as $field => $mult) {
                  // Nulls first.
                  if (is_null($a->$field) && !is_null($b->$field)) {
                      return -$mult;
                  }
                  if (is_null($b->$field) && !is_null($a->$field)) {
                      return $mult;
                  }
  
                  if (is_string($a->$field) || is_string($b->$field)) {
                      // String fields.
                      if ($cmp = strcoll($a->$field, $b->$field)) {
                          return $mult * $cmp;
                      }
                  } else {
                      // Int fields.
                      if ($a->$field > $b->$field) {
                          return $mult;
                      }
                      if ($a->$field < $b->$field) {
                          return -$mult;
                      }
                  }
              }
              return 0;
          });
      }
code_tokens: protected static function sort_records ( & $ records , $ sortfields ) { if ( empty ( $ records ) ) { return ; } // If sorting by course display name, calculate it (it may be fullname or shortname+fullname). if ( array_key_exists ( 'displayname' , $ sortfields ) ) { foreach ( $ records as $ key => $ record ) { if ( ! isset ( $ record -> displayname ) ) { $ records [ $ key ] -> displayname = get_course_display_name_for_list ( $ record ) ; } } } // Sorting by one field - use core_collator. if ( count ( $ sortfields ) == 1 ) { $ property = key ( $ sortfields ) ; if ( in_array ( $ property , array ( 'sortorder' , 'id' , 'visible' , 'parent' , 'depth' ) ) ) { $ sortflag = core_collator :: SORT_NUMERIC ; } else if ( in_array ( $ property , array ( 'idnumber' , 'displayname' , 'name' , 'shortname' , 'fullname' ) ) ) { $ sortflag = core_collator :: SORT_STRING ; } else { $ sortflag = core_collator :: SORT_REGULAR ; } core_collator :: asort_objects_by_property ( $ records , $ property , $ sortflag ) ; if ( $ sortfields [ $ property ] < 0 ) { $ records = array_reverse ( $ records , true ) ; } return ; } // Sort by multiple fields - use custom sorting. uasort ( $ records , function ( $ a , $ b ) use ( $ sortfields ) { foreach ( $ sortfields as $ field => $ mult ) { // Nulls first. if ( is_null ( $ a -> $ field ) && ! is_null ( $ b -> $ field ) ) { return - $ mult ; } if ( is_null ( $ b -> $ field ) && ! is_null ( $ a -> $ field ) ) { return $ mult ; } if ( is_string ( $ a -> $ field ) || is_string ( $ b -> $ field ) ) { // String fields. if ( $ cmp = strcoll ( $ a -> $ field , $ b -> $ field ) ) { return $ mult * $ cmp ; } } else { // Int fields. if ( $ a -> $ field > $ b -> $ field ) { return $ mult ; } if ( $ a -> $ field < $ b -> $ field ) { return - $ mult ; } } } return 0 ; } ) ; }
docstring: Sorts list of records by several fields
  
  @param array $records array of stdClass objects
  @param array $sortfields assoc array where key is the field to sort and value is 1 for asc or -1 for desc
  @return int
docstring_tokens: Sorts list of records by several fields