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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:47:50+00:00 2026-05-20T09:47:50+00:00

I have an array of objects which I want to sort based on one

  • 0

I have an array of objects which I want to sort based on one property, and then kind of ‘group together’ based on another property. In the example below, I would like them sorted based on $sequence, and grouped based on $artist.

// a stripped down version of my class :

<?php
class myClass
{
    public $sequence;
    public $artist;

    function __construct($sequence,$artist)
    {
        $this->sequence=$sequence;
        $this->artist=$artist;
    }

    static function cmp_myclass_sequence($a, $b)
    {
        $a_seq = $a->sequence;
        $b_seq = $b->sequence;
        if ($a_seq == $b_seq) {
            return 0;
        }
        return ($a_seq > $b_seq) ? +1 : -1;
    }
    static function cmp_myclass_artist($a, $b)
    {
        $a_art = strtolower($a->artist);
        $b_art = strtolower($b->artist);
        if ($a_art == $b_art) {
            return 0;
        }
        return ($a_art > $b_art) ? +1 : -1;
    }
}

//some objects of my class which am using for testing :

$myObj1 = new myClass(1,'A');
$myObj2 = new myClass(1,'B');
$myObj3 = new myClass(1,'C');
$myObj4 = new myClass(2,'A');
$myObj5 = new myClass(2,'B');
$myObj6 = new myClass(2,'C');
$myObj7 = new myClass(4,'A');
$myObj8 = new myClass(5,'A');
$myObj9 = new myClass(3,'B');
$myObj10 = new myClass(3,'G');
$myObj11= new myClass(3,'A');

$myArr=array($myObj1,$myObj2,$myObj3,$myObj4,$myObj5,$myObj6,$myObj7,$myObj8,$myObj9,$myObj10,$myObj11);

echo "My Array Before sort : <br>";
foreach ($myArr as $obj){ print_r($obj); echo "<br>"; }

usort($myArr, array("myClass", "cmp_myclass_sequence"));
//usort($myArr, array("myClass", "cmp_myclass_artist"));


echo "My Array AFTER sort : <br>";
foreach ($myArr as $obj){ print_r($obj); echo "<br>";}

?>

Both the sorting functions that I have in the class, cmp_myclass_sequence and cmp_myclass_artist work well on their own. But I am not sure how to get the array grouped in this way :
All the A artists come first, then the B and so on.
Amongst the A artists, the objects should be sorted by the sequence int of that object.

In short this is the result that am looking for :

/*
My Array AFTER sort : 

myClass Object ( [sequence] => 1 [artist] => A )
myClass Object ( [sequence] => 2 [artist] => A )
myClass Object ( [sequence] => 3 [artist] => A )
myClass Object ( [sequence] => 4 [artist] => A )
myClass Object ( [sequence] => 5 [artist] => A )
myClass Object ( [sequence] => 1 [artist] => B )
myClass Object ( [sequence] => 2 [artist] => B )
myClass Object ( [sequence] => 3 [artist] => B )
myClass Object ( [sequence] => 1 [artist] => C )
myClass Object ( [sequence] => 2 [artist] => C )
myClass Object ( [sequence] => 3 [artist] => G ) 
*/
  • 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-20T09:47:51+00:00Added an answer on May 20, 2026 at 9:47 am

    You can do this easily with one compare function:

    function sort_c_arr($item_1, $item_2)
    {
       if($item_1->artist != $item_2->artist)
       {
          return strcmp($item_1->artist, $item_2->artist);
       }
       return $item_1->sequence - $item_2->sequence;
    }
    // ...
    usort($arr, 'sort_c_arr');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of 1000-2000 elements which are pointers to objects. I want
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have an array of objects which populate a UITableView . When a user
I have an array of FileReference objects which have several listeners attached to each
I have a 2-dimensional array of objects and I basically want to databind each
I have an array with arrays in it, where I want to sort the
I have a method which accept jagged array of Objects. public void MyDataBind(object[][] data)
I have a base class object array into which I have typecasted many different
I have an Array of Objects that need the duplicates removed/filtered. I was going
I have an array of Foo objects. How do I remove the second element

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.