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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:45:58+00:00 2026-06-06T14:45:58+00:00

Just wondering if anyone can optimize this usortMonths() function to be better? Basically I

  • 0

Just wondering if anyone can optimize this usortMonths() function to be better? Basically I have an array of months unsorted, $bob, and I want them sorted by month, not alphabetically.

The code works fine, but its my first time with uSort and I wouldnt mind if anyone can give any suggestions of improvement?

function usortMonths($a, $b) {
$months = array('JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER');
    $key = array(0 => 0,1 => 0,2 => FALSE, 3 => FALSE);
    if(array_search(strtoupper($a),$months)!==FALSE) {
        $key[0] = array_search(strtoupper($a),$months);
        $key[2] = TRUE; }
    if(array_search(strtoupper($b),$months)!==FALSE) {
        $key[1] = array_search(strtoupper($b),$months);
        $key[3] = TRUE; }
    if($key[2] && $key[3]){
        if($key[0] < $key[1]) {return -1;}
        if($key[0] == $key[1]){return 0;}
        if($key[0] > $key[1]) {return 1;}
    }
}

$bob = array('april','august','december','february','January','july','june','march','may','november','october','september',);       
usort($bob,"usortMonths");
var_dump($bob);
  • 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-06T14:45:59+00:00Added an answer on June 6, 2026 at 2:45 pm

    How about this?

    function usortMonths($a, $b) {
        $months = array('JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER');
        return array_search(strtoupper($a), $months) - array_search(strtoupper($b), $months);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if anyone can help me with this slight issue. I'm writing a
Just wondering if anyone has experience with this. I have a plain text in
just wondering if anyone has a suggestion for how I can optimize my simple
Just wondering, anyone can guest the problem. I have joomla + invitex installed, so
Just wondering if anyone can suggest a way to better implement what I am
This is very odd, and I'm just wondering if anyone can explain this to
Just wondering if anyone has any ideas on how we can style templates... Like
Just wondering if anyone else has spotted this: On some user's machines running our
Just wondering if anyone knows of a jQuery sortable plugin that I can use
I am just wondering whether anyone knows where I can get a compiled version

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.