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

  • SEARCH
  • Home
  • 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 7719689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:28:45+00:00 2026-06-01T03:28:45+00:00

Lets say I want to sort some items and need to sort them by

  • 0

Lets say I want to sort some items and need to sort them by priority from highest to lowest (5,3,2,1). So highest priority 5 will end up being stored in $items[0] after the sort. Lets say there’s this code already in place to sort it:

$items = $items->sort(function(Item $a, Item $b)
{            
    if ($a->priority == $b->priority)
    {
        return 0;
    }

    return ($a->priority < $b->priority) ? 1 : -1;        
});

Which somehow works, but I don’t know why it works.

I’m assuming return 0 keeps the compared item in its place. What does return 1 do? Does it move $a up the sorted array closer to array[0]? And return -1 moves $a down the array closer to array[10]?

Or does it actually move the $b variable?


SOLVED

Ok I cracked it with a bit of help from Pauly and phpdev. It actually moves the $b variable, not the $a variable inside the callback function. So I needed to alter the logic so it makes sense. -1 moves $b down the array, 1 moves $b up the array and 0 keeps $b in the same place.

$priorities = array(5, 8, 3, 10, 4, 3, 7);

usort($priorities, function($a, $b)
{
    if ($a == $b)
    {
        // Same priority, keep same
        echo "$a is same as $b, keeping the same\n";
        return 0;
    }
    else if ($a > $b)
    {
        // $a is higher priority, move $b down array
        echo "$a greater than $b, moving $b down array\n";
        return -1;
    }
    else {
        // $a is lower priority, move $b up array
        echo "$b greater than $a, moving $b up array\n";                
        return 1;
    }
});

var_dump($priorities);

This outputs:

10 greater than 8, moving 8 down array
10 greater than 7, moving 10 up array
10 greater than 3, moving 10 up array
10 greater than 4, moving 10 up array
10 greater than 5, moving 10 up array
10 greater than 3, moving 10 up array
10 greater than 8, moving 10 up array
5 greater than 3, moving 3 down array
7 greater than 5, moving 5 down array
8 greater than 5, moving 8 up array
5 greater than 4, moving 4 down array
5 greater than 3, moving 5 up array
5 greater than 4, moving 5 up array
8 greater than 7, moving 8 up array
4 greater than 3, moving 4 up array
3 is same as 3, keeping the same
4 greater than 3, moving 3 down array

array(7) {
  [0]=> int(10)
  [1]=> int(8)
  [2]=> int(7)
  [3]=> int(5)
  [4]=> int(4)
  [5]=> int(3)
  [6]=> int(3)
}
  • 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-01T03:28:47+00:00Added an answer on June 1, 2026 at 3:28 am

    -1 moves it down, 0 leaves it, 1 pushes it up.

    http://www.php.net/manual/en/function.usort.php

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

Sidebar

Related Questions

Lets say I have a view, myView, a view controller, myViewController, and some sort
Lets say you want to create a listing of widgets The Widget Manufacturers all
Lets say I want to define 2 styles. .color_red { color: Red; } .banner
Lets say we want a library of javascript-based pieces of functionality (I'm thinking jquery):
Lets say I want to make few classes to determine behaviour of agents. The
I would appreciate any help on this issue. Lets say I want to load
My simplified and contrived example is the following:- Lets say that I want to
Lets say I've got two interchangeable pieces of code and I want to figure
Lets say I have a single object of type Car which I want to
Lets say I have multiple DataGrids throughout my winform app and I want to

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.