Jump to content

Case Insensitive Ordering of Members


xProteuSx

Recommended Posts

So I've got a list of members, that looks something like this:

 

aardvark

CandyFISH

Anthony55

Beezl3bub

angie13

007_bond

 

I am trying to sort these members alphabetically, but case insensitively as well. 

 

My sorting function looks like this, and sorts an array of arrays based on the first character of the second field of each sub-array:

 

function sortmulti($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) 
    {
        if(is_array($array) && count($array)>0) 
        {
            foreach(array_keys($array) as $key)
            {$temp[$key]=$array[$key][$index];}
            
            if(!$natsort) 
            {
                if ($order=='asc')
                {asort($temp);}
                else   
                    {arsort($temp);}
                }
            else
            {
                if ($case_sensitive===true)
                {natsort($temp);}
                else
                {natcasesort($temp);}
                    
                if($order!='asc')
                {$temp=array_reverse($temp,TRUE);}
                }
                    
            foreach(array_keys($temp) as $key)
            {
                if (is_numeric($key))
                {$sorted[]=$array[$key];}
                else   
                    {$sorted[$key]=$array[$key];}
                }
            return $sorted;
            }
        return $sorted;
        }
 
Then I call the function up like this:  $users_array = sortmulti($users_array, 1, 'asc');
 
In the end I end up with sorting that looks like this:
 

007_bond

Anthony55

Beezl3bub

CandyFISH

aardvark

angie13

 

As you can see the sorting considers all upper case characters to be first, followed by lower case characters.  I am looking for something like this:

 

007_bond

aardvark

angie13

Anthony55

Beezl3bub

CandyFISH

 

Any pointers on how I can do this?  Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/
Share on other sites

I am not sure whether I am not understanding something, or whether this function does not sort multidimensional arrays.

 

When I run this nothing happens (still an unordered mess):

 
$users_array = sortmulti($users_array, 1, 'asc');
sort($users_array, SORT_FLAG_CASE);
 
So then I try this, but get an error:
 
$users_array = sortmulti($users_array, 1, 'asc');
sort($users_array[1], SORT_FLAG_CASE);  //index 1 is the 'username' field in the sub-arrays
 
;)

I am looking at this example:

 

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    
$volume[$key]  = $row['volume'];
    
$edition[$key] = $row['edition'];
}


// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key

array_multisort($volumeSORT_DESC$editionSORT_ASC$data);
?>

 

And also this one:

 

<?php

$array = array('Alpha''atomic''Beta''bank');
$array_lowercase array_map('strtolower'$array);

array_multisort($array_lowercaseSORT_ASCSORT_STRING$array);

print_r($array);
?>

 

However, I don't know whether is possible to combine the two and, if it is, how to do it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.