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 1071633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:44:27+00:00 2026-05-16T20:44:27+00:00

I have an array full of objects from the same class. I’d like to

  • 0

I have an array full of objects from the same class. I’d like to sort this array by optional object field, for instance $case->ID or $case->Sender

Is there a built in flavor of the array_sort() function that does this already, or will I have to write this sort function myself?

Answer doesn’t have to explain in detail – this is more like a yes/no question

Thanks


My failed attempt at usort:

function sortBy($sort)
  {
   usort($this->abuseCases, function($a, $b) {
     if($a->{$sort} > $b->{$sort}) return 1;
     if($a->{$sort} < $b->{$sort}) return -1;
     else return 0;
    });
  }

Another failed attempt:

    function sortBy($sort)
    {
        $this->sortBy = $sort;

        usort($this->abuseCases, array("this", "srt"));
    }

    private function srt($a, $b)
    {
        if($a->{$this->sortBy} > $b->{$this->sortBy}) return 1;
        if($a->{$this->sortBy} < $b->{$this->sortBy}) return -1;
        else return 0;
    }

Edit for bump

  • 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-05-16T20:44:28+00:00Added an answer on May 16, 2026 at 8:44 pm

    You can use not only an anonymous function but a closure, like e.g.

    function ($a,$b) use ($sort) { ... }
    

    and $sort will be available in the function body.
    Self-contained example:

    <?php
    function getFn($sort) {
      return function($a, $b) use($sort) {
        if($a->$sort > $b->$sort) return 1;
        if($a->$sort < $b->$sort) return -1;
        return 0;
      };
    }
    
    $abuseCases = array(
      (object)array('ID'=>1, 'Sender'=>'Sender A'),
      (object)array('ID'=>3, 'Sender'=>'Sender D'),
      (object)array('ID'=>2, 'Sender'=>'Sender C'),
      (object)array('ID'=>4, 'Sender'=>'Sender B')
    );
    
    echo "\n----- Sort By Sender ----\n";
    usort($abuseCases, getFn('Sender'));
    foo($abuseCases);
    
    echo "\n----- Sort By ID ----\n";
    usort($abuseCases, getFn('ID'));
    foo($abuseCases);
    
    function foo($a) {
      foreach($a as $o) {
        echo $o->ID, ' ', $o->Sender, "\n";
      }
    }
    

    prints

    ----- Sort By Sender ----
    1 Sender A
    4 Sender B
    2 Sender C
    3 Sender D
    
    ----- Sort By ID ----
    1 Sender A
    2 Sender C
    3 Sender D
    4 Sender B
    

    Update: With php<5.3 you can use an object instead of an anonymous function.
    usort() expects the second parameter to be a callable. That can be an anoymous function as of php 5.3, but it can also be the name of a function ….or an object and a method name passed as an array like array($obj, 'methodName').

    $abuseCases = getData();
    echo "\n----- Sort By Sender ----\n";
    usort($abuseCases, array(new Foo('Sender'), 'compare'));
    foo($abuseCases);
    
    echo "\n----- Sort By ID ----\n";
    usort($abuseCases, array(new Foo('ID'), 'compare'));
    foo($abuseCases);
    
    class Foo {
      public $propName; // protected?
      public function __construct($propertyName) {
        $this->propName = $propertyName;
      }
      public function compare($a, $b) {
        $prop = $this->propName;
        if($a->$prop > $b->$prop) return 1;
        if($a->$prop < $b->$prop) return -1;
        return 0;
      }
    }
    
    function foo($a) {
      foreach($a as $o) {
        echo $o->ID, ' ', $o->Sender, "\n";
      }
    }
    function getData() {
      return array(
        (object)array('ID'=>1, 'Sender'=>'Sender A'),
        (object)array('ID'=>3, 'Sender'=>'Sender D'),
        (object)array('ID'=>2, 'Sender'=>'Sender C'),
        (object)array('ID'=>4, 'Sender'=>'Sender B')
      );
    }
    

    (If you make heavy use of this – or don’t want to write excuses like this one -_- – you might want to define an interface like interface Comparator { ... }, let Foo implement that interface and have some function/class that uses Comparator, i.e. a wrapper for objects around usort().)

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

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Does LINQ to XML count? May 17, 2026 at 2:16 am
  • Editorial Team
    Editorial Team added an answer If all you've done is created the database, you may… May 17, 2026 at 2:16 am
  • Editorial Team
    Editorial Team added an answer The Queryable.OrderBy extension method takes a parameter of type Expression<Func<TSource,… May 17, 2026 at 2:16 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a complex table pulled from a multi-ActiveRecord object array. This listing is
I have setup an array full of words as objects. I am trying then
I currently have an array full of data which from what I believe is
I have this array that I am having trouble traversing: print_r($menu) gives this: [Dashboard]
So I have a JavaScript array going somewhat like screw cap bottle [tech.] nonreturnable
I have a very complex MySQL query that includes use of the same subquery
Does anyone have an explanation of how javascript could do this. Somehow this function
My Address class has a geocode class method that returns an array of address
I need help with pointers and memory management. I need to store different objects,
I have a series of large text files (up to 1 gig) that are

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.