Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up double concatenation of masort.

  • Loading branch information...
commit e39f3d5e3d28a52b04e72fd872412a022d069636 1 parent ec7a00f
@bakert authored
Showing with 0 additions and 72 deletions.
  1. +0 −72 masort.php
View
72 masort.php
@@ -73,23 +73,6 @@ function get_comparison_function($key, $data) {
return 'strcasecmp';
}
-/* test data
-$data = array(
- array('A' => 'lemon',
- 'B' => 'chicken'),
- array('A' => 'orange',
- 'B' => 'duck'),
- array('A' => 'lemon',
- 'B' => 'sherbert'),
- array('A' => 'orange',
- 'B' => 'juice')
-);
-
-print "<pre>";
-masort("A_a,B_a", $data);
-print_r($data);
-*/
-
function numcmp($a, $b) {
if ($a > $b) {
return 1;
@@ -100,51 +83,6 @@ function numcmp($a, $b) {
}
}
-?>
-23:18:26 bluebone@base:~$ cat public_html/masort.txt
-<?php
-
-// Expects Multidimensional associative array for the first param.
-// Second param is "field_a,field_d,field_a" as first param - _a for sort
-// ascending, _d for sort descending.
-function masort(&$data, $sort) {
- if (!$sort || !$data) {
- return $data;
- }
- $function = create_sort_function($sort, $data);
- return uasort($data, $function);
-}
-
-function create_sort_function($sort, $data) {
- $f = '';
- foreach (explode(",", $sort) as $raw) {
- $ending = substr($raw, -strlen("_d"), strlen("_d"));
- if ($ending !== '_a' && $ending !== '_d') {
- $ending = '';
- }
- $key = substr($raw, 0, strlen($raw) - strlen($ending));
- $desc = ($ending === "_d");
- $cmp = get_comparison_function($key, $data);
- $f .= '$res = ' . $cmp . '($a["' . $key . '"], $b["' . $key . '"]); '
- . 'if ($res != 0) { '
- . 'return ' . ($desc ? '-$res' : '$res') . '; '
- . '} ';
- }
- $f .= 'return $a;';
- return create_function('$a, $b', $f);
-}
-
-//Look at the data and guess what the best comparator is.
-function get_comparison_function($key, $data) {
- foreach ($data as $row) {
- $value = $row[$key];
- if (is_numeric($value)) {
- return 'numcmp';
- }
- }
- return 'strcasecmp';
-}
-
/* test data
$data = array(
array('A' => 'lemon',
@@ -162,16 +100,6 @@ function get_comparison_function($key, $data) {
print_r($data);
*/
-function numcmp($a, $b) {
- if ($a > $b) {
- return 1;
- } elseif ($b > $a) {
- return -1;
- } else {
- return 0;
- }
-}
-
/*
Copyright (c) 2004 Thomas David Baker

0 comments on commit e39f3d5

Please sign in to comment.
Something went wrong with that request. Please try again.