Question : sorting array by group

hi,

i'm trying to sort an array of associative arrays, based on one of the keys.

if the value of the key is a primitive, normal sorting occurs - this works fine.

if the value of the key is an array, i'd like to have it sorted by "groups"

some example code is attached

the sorted_by_name bit is fine - but for sorted_by_list, what i want returned is all the array ordered as follows:

all those with "1" (or the lowest if none have "1") in it's list value first, then all those remaining that have the next highest ("2"), etc.

so right now, the returns for sort_by_list look like this:

Array
(
    [0] => Array
        (
            [name] => g
            [list] => Array
                (
                    [0] => 1
                )

        )

    [1] => Array
        (
            [name] => f
            [list] => Array
                (
                    [0] => 2
                )

        )

    [2] => Array
        (
            [name] => b
            [list] => Array
                (
                    [0] => 3
                )

        )

    [3] => Array
        (
            [name] => h
            [list] => Array
                (
                    [0] => 1
                    [1] => 2
                )

        )

    [4] => Array
        (
            [name] => a
            [list] => Array
                (
                    [0] => 1
                    [1] => 3
                )

        )

    [5] => Array
        (
            [name] => e
            [list] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

    [6] => Array
        (
            [name] => d
            [list] => Array
                (
                    [0] => 3
                    [1] => 4
                )

        )

    [7] => Array
        (
            [name] => c
            [list] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )

        )

)


whereas i'd like to get back:


Array
(
    [0] => Array
        (
            [name] => g
            [list] => Array
                (
                    [0] => 1
                )

        )

    [1] => Array
        (
            [name] => h
            [list] => Array
                (
                    [0] => 1
                    [1] => 2
                )

        )

    [2] => Array
        (
            [name] => c
            [list] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )

        )

    [3] => Array
        (
            [name] => a
            [list] => Array
                (
                    [0] => 1
                    [1] => 3
                )

        )

    [4] => Array
        (
            [name] => f
            [list] => Array
                (
                    [0] => 2
                )

        )

    [5] => Array
        (
            [name] => e
            [list] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

    [6] => Array
        (
            [name] => b
            [list] => Array
                (
                    [0] => 3
                )

        )  

    [7] => Array
        (
            [name] => d
            [list] => Array
                (
                    [0] => 3
                    [1] => 4
                )

        )

)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
<?php

$data[] = array('name' => 'h', 'list' => array(1,2));
$data[] = array('name' => 'g', 'list' => array(1));
$data[] = array('name' => 'a', 'list' => array(1,3));
$data[] = array('name' => 'f', 'list' => array(2));
$data[] = array('name' => 'e', 'list' => array(2,3));
$data[] = array('name' => 'b', 'list' => array(3));
$data[] = array('name' => 'c', 'list' => array(1,2,3,4));
$data[] = array('name' => 'd', 'list' => array(3,4));

function deep_sort($array, $sorton){
	
	usort($array, function($a, $b) use($sorton) {
	
		$a = $a[$sorton];
		$b = $b[$sorton];
		
		if(is_array($a) && is_array($b)){
		
			// this bit is obviously flawed - only included for illustrative purposes
			$a = implode('', $a);
			$b = implode('', $b);
			
		}
		
		return ($a == $b) ? 0 : ($a > $b) ? 1 : -1;
	
	});
	
	return $array;

}

$sorted_by_name = deep_sort($data, 'name');

print '<pre>';

print_r($sorted_by_name);

print '</pre>';

$sorted_by_list = deep_sort($data, 'list');

print '<pre>';

print_r($sorted_by_list);

print '</pre>';

?>

Answer : sorting array by group

Look this over and see if it makes sense to you.  Best, ~Ray
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
<?php // RAY_temp_usort.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$data[] = array('name' => 'h', 'list' => array(1,2));
$data[] = array('name' => 'g', 'list' => array(1));
$data[] = array('name' => 'a', 'list' => array(1,3));
$data[] = array('name' => 'f', 'list' => array(2));
$data[] = array('name' => 'e', 'list' => array(2,3));
$data[] = array('name' => 'b', 'list' => array(3));
$data[] = array('name' => 'c', 'list' => array(1,2,3,4));
$data[] = array('name' => 'd', 'list' => array(3,4));


// A FUNCTION TO COMPARE BY NAMES
function cmp_name($a, $b)
{
    if ($a["name"] == $b["name"]) return 0;
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

// A FUNCTION TO COMPARE BY GROUPS
function cmp_group($a, $b)
{
    if ($a["group"] == $b["group"]) return 0;
    return ($a["group"] < $b["group"]) ? -1 : 1;
}



// COPY ARRAY AND SORT ON NAME
$test = $data;
usort($test, 'cmp_name');

// SHOW THE WORK PRODUCT
var_dump($test);




// COPY THE ARRAY AND ADD GROUPS
$test = $data;
$maxlen = 0;
foreach($test as $ptr => $arr)
{
    $test[$ptr]['group'] = implode('', $arr['list']);
    if (strlen($test[$ptr]['group']) > $maxlen) $maxlen = strlen($test[$ptr]['group']);
}

// PAD THE GROUPS TO CONSISTENT LENGTH
foreach ($test as $ptr => $arr)
{
    $test[$ptr]['group'] = str_pad($test[$ptr]['group'], $maxlen+1, '0');
}

// SORT ON GROUPS
usort($test, 'cmp_group');

// REMOVE THE GROUPS (IF NEEDED)
foreach ($test as $ptr => $arr)
{
    unset($test[$ptr]['group']);
}

// SHOW THE WORK PRODUCT
var_dump($test);
Random Solutions  
 
programming4us programming4us