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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:24:34+00:00 2026-05-29T11:24:34+00:00

I have a problem with PHP usort() . Let’s suppose I have an array

  • 0

I have a problem with PHP usort(). Let’s suppose I have an array like this (that’s a simplification, I’ve not to work with names, and I have an array of objects, not arrays):

$data = array(
    array('name' => 'Albert',      'last' => 'Einstein'),
    array('name' => 'Lieserl',     'last' => 'Einstein'),
    array('name' => 'Alan',        'last' => 'Turing'  ),
    array('name' => 'Mileva',      'last' => 'Einstein'),
    array('name' => 'Hans Albert', 'last' => 'Einstein')
);

As you can see, the array is sorted arbitrarily.

Now, if want to sort it by last, I do:

function sort_some_people($a, $b) { return strcmp($a['last'], $b['last']); }
usort($data, 'sort_some_people');

And I have:

Array (
    [0] => Array ( [name] => Mileva       [last] => Einstein )
    [3] => Array ( [name] => Albert       [last] => Einstein )
    [1] => Array ( [name] => Lieserl      [last] => Einstein )
    [2] => Array ( [name] => Hans Albert  [last] => Einstein )
    [4] => Array ( [name] => Alan         [last] => Turing   )
)

That is ok, now they are sorted by last. But, as you can see, I’ve just completely lost the previous sorting. What am I saying? I want to preserve array sorting as it was before, but as a secondary sorting. I hope I was clear. Practically I want to sort data using something as usort() (so, a completely custom sorting), but if sorting field is identical between two items, I want to keep their relative position as it was before. Considering the given example, I want Lieserl Einstein to appear before Mileva Einstein because it was like this at beginning.

  • 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-29T11:24:35+00:00Added an answer on May 29, 2026 at 11:24 am

    The sorting algorithms used in PHP have this property where the order is undefined if the items match.

    If you need to keep the order, then you have to roll your own code.

    Fortunately some one already has:
    http://www.php.net/manual/en/function.usort.php#38827

    $data = array(
        array('name' => 'Albert',      'last' => 'Einstein'),
        array('name' => 'Lieserl',     'last' => 'Einstein'),
        array('name' => 'Alan',        'last' => 'Turing'  ),
        array('name' => 'Mileva',      'last' => 'Einstein'),
        array('name' => 'Hans Albert', 'last' => 'Einstein')
    );
    
    function sort_some_people($a, $b) {
            return strcmp($a['last'], $b['last']);
    }
    
    function mergesort(&$array, $cmp_function = 'strcmp') {
        // Arrays of size < 2 require no action.
        if (count($array) < 2) return;
        // Split the array in half
        $halfway = count($array) / 2;
        $array1 = array_slice($array, 0, $halfway);
        $array2 = array_slice($array, $halfway);
        // Recurse to sort the two halves
        mergesort($array1, $cmp_function);
        mergesort($array2, $cmp_function);
        // If all of $array1 is <= all of $array2, just append them.
        if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) {
            $array = array_merge($array1, $array2);
            return;
        }
        // Merge the two sorted arrays into a single sorted array
        $array = array();
        $ptr1 = $ptr2 = 0;
        while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
            if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) {
                $array[] = $array1[$ptr1++];
            }
            else {
                $array[] = $array2[$ptr2++];
            }
        }
        // Merge the remainder
        while ($ptr1 < count($array1)) $array[] = $array1[$ptr1++];
        while ($ptr2 < count($array2)) $array[] = $array2[$ptr2++];
        return;
    }
    
    mergesort($data, 'sort_some_people');
    
    print_r($data);
    

    Output:

    Array
    (
        [0] => Array
            (
                [name] => Albert
                [last] => Einstein
            )
    
        [1] => Array
            (
                [name] => Lieserl
                [last] => Einstein
            )
    
        [2] => Array
            (
                [name] => Mileva
                [last] => Einstein
            )
    
        [3] => Array
            (
                [name] => Hans Albert
                [last] => Einstein
            )
    
        [4] => Array
            (
                [name] => Alan
                [last] => Turing
            )
    
    )
    

    Voila!

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

Sidebar

Related Questions

I have problem with php's preg_match.I want to make something like this : When
So I have this problem in PHP, I have a class called unit that
I have this problem that my .php file is presented as clear text in
I have a database that has a url colomn like this-is-the-title.php I want to
I have this sample page with links that look like buttons: http://www.problemio.com/problems/problem.php?problem_id=251 Most of
I have problem with session in cakephp.I have one file chat.php that is in
I have the problem, that PHP replaces all spaces with underscores in POST and
i have a problem with php in arrays. i have a db table like
i have problem at php $_POST code. this is the my codes? what i
A have a problem with PHP application, that I'm working on. Main page which

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.