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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:22:46+00:00 2026-06-18T14:22:46+00:00

I have tried sorting the below array to keep the even values on top

  • 0

I have tried sorting the below array to keep the even values on top and in sorted order from asc to desc

$array = array(1,2,3,4,5,6,7,8,9,10);

I tried this to sort the array

usort($array, function($a, $b) { 
                    if ($a % 2 == 0 )
                    { 
                        return 1 ;
                    }
                    else 
                    { 
                        return -1;
                    }
            }
        );

I got the output like below

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

And I want the output array to be

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

The even and odd values should be sorted in asc to desc order but keep the even values on top of odd values

  • 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-18T14:22:47+00:00Added an answer on June 18, 2026 at 2:22 pm

    usort is not stable. The documentation states:

    If two members compare as equal, their relative order in the sorted array is undefined.

    So, what you can do is:

    usort($array, function($a, $b) {
                    if ($a % 2 == $b % 2) {
                        return intval($a - $b);
                    }
                    elseif ($a % 2 == 0 )
                    { 
                        return -1 ;
                    }
                    else 
                    { 
                        return 1;
                    }
            }
        );
    

    This compares the actual values if both are even or both are odd.

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

Sidebar

Related Questions

I have to sort this array in descening order with the key likecount. How
I tried implementing a sorting program in mapreduce such that I have just the
Have tried to find solutions for this and can't really come up with anything.
I have tried searching over the internet about this problem but not able to
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have sorted collection (List) and I need to keep it sorted at all
Can I get some help please? I have tried many methods to get this
This is one of those hard to explain questions. I've tried my best below,
I have a problem with sorting of an array. $infoGroup is the result of
Possible Duplicate: Sorting an associative array in PHP I have an array that looks

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.