MOTIF-Z Productions
MOTIF-Z Productions
MOTIF-Z • Gallery • Cape Thong
MOTIF-Z • Gallery • London Vacation
MOTIF-Z • Gallery • Family Album
MOTIF-Z • Gallery • March Madness
MOTIF-Z • Gallery • Green Day
MOTIF-Z • Gallery • United Nations
MOTIF-Z • Gallery • Wikimedia Commons
MOTIF-Z • Gallery • Vincent van Gogh
MOTIF-Z • Gallery • Simply Recipes
MOTIF-Z • Gallery • Body & Sole
MOTIF-Z • Gallery • Our Portfolio
MOTIF-Z • Gallery • Superman Returns

MOTIF-Z Record - Literal - Selected Languages - Bubble Sorts - All Things Creative

FWD: send page to...printer friendly page...

And now
  for something
  completely different...

Fractal Matrix - Mandelbrot Fractal

William Shakespeare
All's Well That Ends Well [1601-1603]

Link to BlinkList Link to Yahoo! Buzz Link to Delicious Link to Digg Link to Diigo Link to Facebook Link to Google Link to Kaboodle Link to Kirtsy Link to LinkedIn Link to MisterWong Link to Mixx Link to Netvouz Link to NewsVine Link to Propeller Link to Reddit Link to Segnalo Link to Slashdot Link to Sphinn Link to StumbleUpon Link to Technorati Link to Twitter Link to YahooMyWeb Link to AddThis

Selected Languages

Bubble Sorts...

order by dialect: phporder by dialect: php

Sort an array (a table) of associative arrays based on an secondary (sort_key) column name is a simple SQL request...

    SELECT * FROM table ORDER BY sort_key ASC

The sortf family of PHP script functions uses a bubble sort method to order the table with equivalent results...

<?php
//###############################################################################################################
// Copyright © MOTIF-Z Productions, Inc. All Rights Reserverd.
//###############################################################################################################

    include_once ('/include/pragma.php');
    use ('4.3.0');

//---------------------------------------------------------------------------------------------------------------
function sortby ($_0, $_1) {
//---------------------------------------------------------------------------------------------------------------
//  syntax: int sortby (mixed valueA, mixed valueB)
//      returns -1, 0, or 1 depending on whether valueA sorts less than, equal to, or greater than valueB...

    return (($_0 == $_1) ? 0 : ($_0 < $_1) ? -1 : 1);
}
function sortby_a  ($_0, $_1) { return (sortby ($_0, $_1)); }
function sortby_ai ($_0, $_1) { return (sortby (strtoupper ($_0), strtoupper ($_1))); }
function sortby_d  ($_0, $_1) { return (sortby ($_1, $_0)); }
function sortby_di ($_0, $_1) { return (sortby (strtoupper ($_1), strtoupper ($_0))); }

//---------------------------------------------------------------------------------------------------------------
function sortf () {
//---------------------------------------------------------------------------------------------------------------
//  syntax: array sortf (array table, string sort_key, function sort_by)
//      returns an array sorted by the values of the sort_key column name...

    $_es = array ();
    list (

        $_ds,
        $_bx,
        $_ip

    ) = func_get_args ();
    $_ss = array_keys ($_ds);

    for ($_bp = count ($_ss) - 1; $_bp > 0; $_bp--) {

        for ($_sp = 0; $_sp < $_bp; $_sp++) {

            if ($_ip ($_ds[$_ss[$_sp + 1]][$_bx], $_ds[$_ss[$_sp]][$_bx]) < 0) {

                $_ax = $_ss[$_sp];
                $_ss[$_sp] = $_ss[$_sp + 1];
                $_ss[$_sp + 1] = $_ax;
            }
        }
    }
    foreach ($_ss as $_dx) {

        $_es[$_dx] = $_ds[$_dx];
    }
    return ($_es);
}
function sortfa  ($_0, $_1) { return (sortf ($_0, $_1, sortby_a )); }
function sortfai ($_0, $_1) { return (sortf ($_0, $_1, sortby_ai)); }
function sortfd  ($_0, $_1) { return (sortf ($_0, $_1, sortby_d )); }
function sortfdi ($_0, $_1) { return (sortf ($_0, $_1, sortby_di)); }

//---------------------------------------------------------------------------------------------------------------
/* ORDER_BY:*/ {
//---------------------------------------------------------------------------------------------------------------

    $_ds = array (

        '_A' => array ( '_X' => 3, '_Y' => 'b', '_Z' => 'abc' ),
        '_B' => array ( '_X' => 2, '_Y' => 'c', '_Z' => 'mno' ),
        '_C' => array ( '_X' => 1, '_Y' => 'a', '_Z' => 'xyz' )
    );
    $_es = sortfa ($_ds, '_Y');
    ...
    ...
}
exit (0);

//###############################################################################################################
?>

order by dialect: javascriptorder by dialect: javascript

Sort an array (a table) of associative arrays based on an secondary (sort_key) column name is a simple SQL request...

    SELECT * FROM table ORDER BY sort_key ASC

The sortf family of JavaScript functions uses a bubble sort method to order the table with equivalent results...

<script type="text/javascript">
//<![CDATA[
<!--
//###############################################################################################################
// Copyright © MOTIF-Z Productions, Inc. All Rights Reserverd.
//###############################################################################################################

//---------------------------------------------------------------------------------------------------------------
function sortby (_0, _1) {
//---------------------------------------------------------------------------------------------------------------
//  syntax: int sortby (mixed valueA, mixed valueB)
//      returns -1, 0, or 1 depending on whether valueA sorts less than, equal to, or greater than valueB...

    return ((_0 == _1) ? 0 : (_0 < _1) ? -1 : 1);
}
function sortby_a  (_0, _1) { return (sortby (_0, _1)); }
function sortby_ai (_0, _1) { return (sortby (_0.toUpperCase (), _1.toUpperCase ())); }
function sortby_d  (_0, _1) { return (sortby (_1, _0)); }
function sortby_di (_0, _1) { return (sortby (_1.toUpperCase (), _0.toUpperCase ())); }

//---------------------------------------------------------------------------------------------------------------
function sortf () {
//---------------------------------------------------------------------------------------------------------------
//  syntax: array sortf (array table, string sort_key, function sort_by)
//      returns an array sorted by the values of the sort_key column name...

    var _es = {};

    var _ds = arguments[0];
    var _bx = arguments[1];
    var _ip = arguments[2];

    var _ss = [];

    for (var _dx in _ds) {

        _ss.push (_dx);
    }
    for (var _bp = _ss.length - 1; _bp > 0; _bp--) {

        for (var _sp = 0; _sp < _bp; _sp++) {

            if (_ip (_ds[_ss[_sp + 1]][_bx], _ds[_ss[_sp]][_bx]) < 0) {

                _ax = _ss[_sp];
                _ss[_sp] = _ss[_sp + 1];
                _ss[_sp + 1] = _ax;
            }
        }
    }
    for (var _dx in _ss) {

        _es[_ss[_dx]] = _ds[_ss[_dx]];
    }
    return (_es);
}
function sortfa  (_0, _1) { return (sortf (_0, _1, sortby_a )); }
function sortfai (_0, _1) { return (sortf (_0, _1, sortby_ai)); }
function sortfd  (_0, _1) { return (sortf (_0, _1, sortby_d )); }
function sortfdi (_0, _1) { return (sortf (_0, _1, sortby_di)); }

//---------------------------------------------------------------------------------------------------------------
ORDER_BY: {
//---------------------------------------------------------------------------------------------------------------

    var _ds = {

        '_A' : { '_X' : 3, '_Y' : 'b', '_Z' : 'abc' },
        '_B' : { '_X' : 2, '_Y' : 'c', '_Z' : 'mno' },
        '_C' : { '_X' : 1, '_Y' : 'a', '_Z' : 'xyz' }
    };
    var _es = sortfa (_ds, '_Y');
    ...
    ...
}

//###############################################################################################################
// -->
//]]>
</script>

order by dialect: perlorder by dialect: perl

Sort an array (a table) of associative arrays based on an secondary (sort_key) column name is a simple SQL request...

    SELECT * FROM table ORDER BY sort_key ASC

The sortf family of Perl script functions uses a bubble sort method to order the table with equivalent results...

#!/usr/local/bin/perl -T

#################################################################################################################
#  Copyright © MOTIF-Z Productions, Inc. All Rights Reserverd.
#################################################################################################################

    use 5.004;
    use strict;

# ---------------------------------------------------------------------------------------------------------------

sub sortby_a  { return ($a cmp $b); }
sub sortby_ai { return (uc ($a) cmp uc ($b)); }
sub sortby_d  { return ($b cmp $a); }
sub sortby_di { return (uc ($b) cmp uc ($a)); }

# ---------------------------------------------------------------------------------------------------------------
sub sortf {
# ---------------------------------------------------------------------------------------------------------------
#   syntax: array sortf (array table, string sort_key, function sort_by)
#       returns an array sorted by the values of the sort_key column name...

    my $es;
    (my (

        $ds,
        $bx,
        $ip

    )) = @_;
    my $ss;

    @{$ss} = keys (%{$ds});

    for (my $bp = scalar (@{$ss}) - 1; $bp > 0; $bp--) {

        for (my $sp = 0; $sp < $bp; $sp++) {

            if ($ip ($ds->{$ss->[$sp + 1][$bx]}, $ds->{$ss->[$sp][$bx]}) < 0) {

                $ax = $ss->[$sp];
                $ss->[$sp] = $ss->[$sp + 1];
                $ss->[$sp + 1] = $ax;
            }
        }
    }
    foreach $dx (@{$ss}) {

        $es->{$dx} = $ds->{$dx};
    }
    return ($es);
}
sub sortfa  ($ds, $bx) { return (sortf ($ds, $bx, sortby_a )); }
sub sortfai ($ds, $bx) { return (sortf ($ds, $bx, sortby_ai)); }
sub sortfd  ($ds, $bx) { return (sortf ($ds, $bx, sortby_d )); }
sub sortfdi ($ds, $bx) { return (sortf ($ds, $bx, sortby_di)); }

//---------------------------------------------------------------------------------------------------------------
ORDER_BY: {
//---------------------------------------------------------------------------------------------------------------

    my $ds = {

        '_A' => { '_X' => 3, '_Y' => 'b', '_Z' => 'abc' },
        '_B' => { '_X' => 2, '_Y' => 'c', '_Z' => 'mno' },
        '_C' => { '_X' => 1, '_Y' => 'a', '_Z' => 'xyz' }
    };
    my $es = sortfa ($ds, '_Y');
    ...
    ...
}
exit (0);

#################################################################################################################

productions
contact us

MOTIF-Z Productions @ facebook MOTIF-Z Productions @ linkedin MOTIF-Z Productions @ twitter MOTIF-Z Productions @ local page feed

archive

MOTIF-Z Archive - Clam and Mussel [1926] Georgia O'Keeffe
February 2012
S M T W T F S
      1 2 3 4
5 6 7 8 9 10 11
12 MOTIF-Z Archive 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      
             

member access

commentary
export
gallery
interactive
logbook
news
portal
record

search

Go To Site
























MOTIF-Z Productions

Questions? Comments? Suggestions?
© 2012 MOTIF-Z Productions, Inc. All Rights Reserved.
Last Modified February 13, 2012

Evaluate URL @ W3C Markup Validation Service Site