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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:16:13+00:00 2026-06-09T14:16:13+00:00

Well, There are many posts and questions using usort to sort array on custom

  • 0

Well,
There are many posts and questions using usort to sort array on custom pattern, but didnt find anything that matches my need.
Here is the code I have so far,

$arrayToSort=array("Sabin","Anil","Cyrus","Kamal","Kesha","Bimal");

function mycmp($a, $b)
{
    static $order = array('A', 'B','C',"Ke'",'Ka','R', 'S');
    return array_search(substr($a,0,1), $order) - array_search(substr($b,0,1), $order);
}

usort($arrayToSort, "mycmp");

The above codes sorts $arrayToSort in following pattern

Array
(
    [0] => Anil
    [1] => Bimal
    [2] => Cyrus
    [3] => Kamal
    [4] => Kesha
    [5] => Sabin
)

However, if you look into my custom pattern static $order = array('A', 'B','C',"Ke'",'Ka','R', 'S'); Kesha should come before Kamal as in my pattern ‘Ke’ comes before ‘Ka’

Again I know substr($a,0,1) takes the first character only that’s why its not working.
I also doubt that I have to take another approach to get job done, but I m not being able to figure it out.

How to sort if the element of array pattern is not consistent, i.e. some of the elements have one character while others have two? Any help will highly be appreciated. Thanks

  • 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-09T14:16:15+00:00Added an answer on June 9, 2026 at 2:16 pm

    Here is a way you can do it. This could certainly be better optimized, but you get the idea.

    <?php
    
    class Accessor {
      static $order = array(
        'A' => 1,
        'B' => 1,
        'C' => 1,
        'K' => array(
          'e' => 1,
          'a' => 1,
        ),
        'R' => 1,
        'S' => 1
      );
    }
    
    $arrayToSort=array("Kamal","Kesha","Sabin","Anil","Cyrus","Bimal");
    
    function mycmp($a, $b, $base) {
      $letter1 = $a[0];
      $letter2 = $b[0];
      if ($letter1 == $letter2) {
        if (is_array($base[$letter1])) {
          return mycmp(substr($a, 1), substr($b, 1), $base[$letter1]);
        }
      }
      return array_search($letter1, array_keys($base)) - array_search($letter2, array_keys($base));
    }
    
    function cmp_proxy($a, $b) {
      return mycmp($a, $b, Accessor::$order);
    }
    
    usort($arrayToSort, "cmp_proxy");
    print_r($arrayToSort);
    

    OUTPUT

    Array
    (
        [0] => Anil
        [1] => Bimal
        [2] => Cyrus
        [3] => Kesha
        [4] => Kamal
        [5] => Sabin
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well is there? From everything I've read, it seems like the answer is no,but
It seems to be well-known there is a bug when using JMenuItem.getRootPane(). I read
Rails has the rest autho plugin which works well but is there a solution
Hii, there are many others had already post so many question about this..But here
Hi I know that there are many questions already in SO related to my
I am interested in starting Mobile application Development.There are many suggestions on internet but,I
Many posts on different aspects of this question but I haven't seen a post
There are many questions and guides on how to get cURL to DO file
well there are 3 activities say Act1 , Act2 and Act3 . in Act1
For each of the modern browsers are there well known hooks I can tie

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.