Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7851783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:15:52+00:00 2026-06-02T19:15:52+00:00

$students = array ( 256 => array (‘name’ => ‘Jon’, ‘grade’ => 98.5), 2

  • 0
    $students = array (
     256 => array ('name' => 'Jon', 'grade' => 98.5),
     2 => array ('name' => 'Vance', 'grade' => 85.1),
     9 => array ('name' => 'Stephen', 'grade' => 94.0),
     364 => array ('name' => 'Steve', 'grade' => 85.1),
     68 => array ('name' => 'Rob', 'grade' => 74.6)
     );

 function grade_sort($x,$y){
  return ($x['grade'] < $y['grade']);
  }
uasort ($students, 'grade_sort');

i am a new learner of php,i can’t understand the above code well. could i change this return ($x['grade'] < $y['grade']); to return $x < $y; if not, why? thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:15:53+00:00Added an answer on June 2, 2026 at 7:15 pm

    could i change this return ($x['grade'] < $y['grade']); to return $x < $y;?

    No. $x and $y will be one of the arrays inside $students.
    E.g. $x may be array('name' => 'Jon', 'grade' => 98.5)
    and $y may be array('name' => 'Vance', 'grade' => 85.1).
    So return $x < $y doesn’t make sense.

    That code isn’t very good though. The comparison function grade_sort takes two elements at a time and should return 0 if both elements are considered equal, a negative number if the first is lower and a positive number if the second lower. Yours returns true or false. The correct code would be:

    function grade_sort($x, $y){
        return $y['grade'] - $x['grade'];
    }
    uasort($students, 'grade_sort');
    

    uasort simply keeps calling this function with two different elements until it is satisfied it knows which element is larger than which and has sorted them all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does this store name and grade to an array correctly? public class Main {
How do i print out female only students from an array. My current code
What I'd like to do is add an array of students to each manager
I want to store all the contents like name and marks for 30 students.
I have the following isAuthorized() function in my students controller: function isAuthorized() { $studentId
I've created a variable length array in one function, however I need to refer
I am trying to pass an array of Student into the function processStudent(string myFilename,
Imagine we have an array of students: Students: Ben Sam ... Every student has
trying to put together a 3-tiered array of term, class, students for a waitlist
I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.