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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:05:35+00:00 2026-05-31T05:05:35+00:00

What I want to do is create a Comparable class, similar to IComparable in

  • 0

What I want to do is create a Comparable class, similar to IComparable in .NET, such that you can instantiate it like so:

$cmp = new MultiArrayCompare(2);

And then you can sort an array via:

usort($myArray, $cmp);

And it will sort an array of arrays on the 2nd index. To do that, I imagine usort would try to call $cmp like a function, so I’d have to override that behaviour somehow. It doesn’t look like __call does what I want (thought for a minute it was like Python’s __call__).

If this isn’t possible… is there another nice way to create a general solution to this problem? Where you can create a user-defined sorting function but give pass it some value (“2” in this case)?


Using __invoke I was able to create these classes:

abstract class Comparable {
    abstract function Compare($a, $b);

    function __invoke($a, $b) {
        return $this->Compare($a, $b);
    }
}

class ArrayCompare extends Comparable {
    private $key;

    function __construct($key) {
        $this->key = $key;  
    }

    function Compare($a, $b) {
        if($a[$this->key] == $b[$this->key]) return 0;
        return $a[$this->key] < $b[$this->key] ? -1 : 1;
    }
}

class ArrayCaseCompare extends Comparable {
    private $key;

    function __construct($key) {
        $this->key = $key;  
    }

    function Compare($a, $b) {
        return strcasecmp($a[$this->key], $b[$this->key]);
    }
}

Which I can use to sort an array of arrays:

$arr = array(
    array(1,2,3),
    array(2,3,4),
    array(3,2,4),
)

usort($arr,new ArrayCompare(1));
  • 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-31T05:05:36+00:00Added an answer on May 31, 2026 at 5:05 am

    You are looking for __invoke:

    The __invoke() method is called when a script tries to call an object as a function.

    Example:

    class SortAscending
    {
        public function __invoke($a, $b)
        {
           return $a - $b;
        }
    }
    
    $numbers = array(4,7,2,3,9,1);
    usort($numbers, new SortAscending);
    print_r( $numbers );
    

    Output (demo):

    Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 7 [5] => 9 ) 
    

    The slimmer alternative is using a Closure. Your code would simply be:

    $arr = array(
        array(1,2,3),
        array(2,3,4),
        array(3,2,4),
    );
    
    $key = 1;
    usort($arr, function($a, $b) use ($key) {
        return $a[$key] - $b[$key];
    });
    

    or – reusable and configurable (demo):

    $ArrayCompare = function ($index) {
        return function($a, $b) use ($index) {
            return $a[$index] - $b[$index];
        };
    };
    usort($arr, $ArrayCompare(1));
    

    Apart from that, you can use basically any method you want and specify the method in the callback:

    usort($arr, array(new ArrayCompare(1), 'Compare'));
    

    The above would achieve the same without magic. See the chapter on callbacks for additional options.

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

Sidebar

Related Questions

I am trying ebook reading app. In that I want create UIActionSheet by clicking
I want to create a bookshelf that tiles horizontally and vertically. I have three
I want to create a simple class in Javascript with only one public function
As titled. We all know if we want a class to be comparable and
I have a generic java class that stores comparables: public class MyGenericStorage<T extends Comparable<T>>
I want to create a tcp connection to a web server using information that
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want create module which update list of usb devices automatically (not only mass

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.