PHP 2-dimensional array sorting algorithms

For 2-dimensional arrays looking like:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => roger
            [age] => 31
        )

    [1] => Array
        (
            [id] => 2
            [name] => brutus
            [age] => 24
        )

    [2] => Array
        (
            [id] => 3
            [name] => ganesh
            [age] => 92
        )

)

I find that comb sort is usually the fastest but its worst case is much worst than quick sort so it could become a bottleneck depending on your data.

<?php

    ini_set( 'memory_limit', '128M' ) ;


    /**
    * @desc bubble_sort for 2 dimensional array (all the arrays of the 2nd dimension need to have the field $sort_by)
    * @param array^2 $data the array of arrays
    * @param string $sort_by the parameter that will be used for comparison
    * @param string $sort_direction "asc": ascending, "desc": descending
    */
    function bubble_sort( $data, $sort_by, $sort_direction ) {
        if( $sort_direction=='asc' ) {
            for( $i=1 ; $i<count($data) ; $i++ ) {
                for( $j=1 ; $j<count($data) ; $j++ ) {
                    if( $data[$j-1][$sort_by]>$data[$j][$sort_by] ) {
                        $temp = $data[$j-1] ;
                        $data[$j-1] = $data[$j] ;
                        $data[$j] = $temp ;
                    }
                }
            }
        } else {
            for( $i=1 ; $i<count($data) ; $i++ ) {
                for( $j=1 ; $j<count($data) ; $j++ ) {
                    if( $data[$j-1][$sort_by]<$data[$j][$sort_by] ) {
                        $temp = $data[$j-1] ;
                        $data[$j-1] = $data[$j] ;
                        $data[$j] = $temp ;
                    }
                }
            }
        }
        return $data ;
    }


    /**
    * @desc comb_sort for 2 dimensional array (all the arrays of the 2nd dimension need to have the field $sort_by)
    * @param array^2 $data the array of arrays
    * @param string $sort_by the parameter that will be used for comparison
    * @param string $sort_direction "asc": ascending, "desc": descending
    */
    function comb_sort( $data, $sort_by, $sort_direction ) {
        $gap   = count( $data ) ;
        $swaps = -1 ;
        while( !($gap<=1 && $swaps==0) ) {
            if( $gap>1 ) {
                $gap = $gap/1.3 ;
                if( $gap==10 || $gap==9 ) {
                    $gap = 11 ;
                }
            }
            $i = 0 ;
            $swaps = 0 ;
            while( !(($i+$gap)>=count($data)) ) {
                if( ($sort_direction=='asc' && $data[$i][$sort_by]>$data[$i+$gap][$sort_by]) ||
                    ($sort_direction=='desc' && $data[$i][$sort_by]<$data[$i+$gap][$sort_by]) ) {
                    $temp = $data[$i] ;
                    $data[$i] = $data[$i+$gap] ;
                    $data[$i+$gap] = $temp ;
                    $swaps = 1 ;
                }
                $i++ ;
            }
        }

        return $data ;
    }


    /**
    * @desc quick_sort for 2 dimensional arrays (all the arrays of the 2nd dimension need to have the field $sort_by)
    * @param array^2 $data the array of arrays
    * @param string $sort_by the parameter that will be used for comparison
    * @param string $sort_direction "asc": ascending, "desc": descending
    */
    function quick_sort( $data, $sort_by, $sort_direction ) {
        if( count($data)<=1 || $sort_by=='' ) {
            return $data ;
        } else {
            $pivot = $data[0][$sort_by] ;
            $x = $y = array() ;
            for( $i=1 ; $i<count($data) ; $i++ ) {
                if( $data[$i][$sort_by]<$pivot ) {
                    if( $sort_direction=="asc" ) {
                        $x[] = $data[$i] ;
                    } else {
                        $y[] = $data[$i] ;
                    }
                } else {
                    if( $sort_direction=="asc" ) {
                        $y[] = $data[$i] ;
                    } else {
                        $x[] = $data[$i] ;
                    }
                }
            }
            return array_merge( quick_sort($x, $sort_by, $sort_direction), array($data[0]), quick_sort($y, $sort_by, $sort_direction) ) ;
        }
    }
?>

Pulling the number keyboard in iOS' Safari while disabling client side input validation

Safari on iOS allows you to pull a specific keyboard for an input field. For example if I have a field expecting numeric input, we’ll make sure that our input has the attribute of “number” rather than the usual “text” as such:

<input type="number"/>

This will save users a few clicks; however Safari forces input validation on the client-side when you do that. Meaning if you wanted a numeric keyboard by default while allowing other characters, you input will fail.

IMG_0798

Not only is it highlighted in red, this.value also returns null as opposed to what is clearly in the field, rendering it unusable.

When the field is set to “-10”

Screen Shot 2013-11-20 at 10.14.10 AM

When the field is set to “+10”

Screen Shot 2013-11-20 at 10.14.03 AM

One would think that explicitly defining a pattern to check for would let Safari know that we are interested in trumping its input validation but such is not the case. As a result, the following does not help our cause:

<input type="number" pattern="(-+){0,1}[0-9]{1,}">

So here’s a completely hackish way to get the keyboard you want with no input validation:

<input type="number" pattern="(-+){0,1}[0-9]{1,}" onFocus="that=this; setTimeout(function(){ that.setAttribute('type','text'); },10);" onBlur="that=this; setTimeout(function(){ that.setAttribute('type','number'); },10);"/>;

That’s right, after iOS pulled the right keyboard, we change the field type to “text”. Note that the client will still display some red around the field as it will perform the validation but at least this.value will return what’s in the friggin’ field.

Note 1: the setTimeout is necessary, if we perform the change immediately onFocus, safari pulls the text keyboard.

Note 2: the that=this indirection is necessary to save a reference of the object to the context of setTimeout.