MOTIF-Zrecordliteral

And now
for something
completely different...

Fractal Matrix @ Mandelbrot Fractal

The web of our life is of a mingled yarn,...

...good and ill together.

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








Selected Languages

Bubble Sorts

order 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. 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: 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. 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: 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. 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);

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

Bubble Sorts...

The web of our life is of a mingled yarn,...

...good and ill together.

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

Productions

home

contact

Calendar

November 2017
S M T W T F S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17
19 20 21 22 23 24 25
26 27 28 29 30    
             

Clients

member access

Variations

commentary
export
gallery
interactive
logbook
news
portal
record

Options

Permalink

Selected Languages @ Bubble Sorts @ MOTIF-Z Productions

calendar | member access | contact

info@motifz.com

© 2017 motif-z productions
last modified: 2015.12.18