473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sorting Array: $value[$s][$i]

Hi there,

how can I sort my array $value[$s][$i]? To be more precise:

Say I have three countries ($s) and each has three values ($i) reprensenting yearly statistics:

DE with 12, 23, 28
FR with 8, 28,15
CH with 10, 20, 30

Now, I want to sort my array by it's highest value. How can I achieve this?

Thanks for any hints!
Jan 22 '07 #1
8 1486
ronverdonk
4,258 Expert 4TB
You did not specify what part of the array you wanted sorted, so I'll give you this snippet:[php]<?php
$a=array('DE' => array(12, 28, 23),
'FR' => array(8, 28,15),
'CH' => array(30, 20, 10) );
echo '<br><pre>'; print_r($a);
foreach ($a as $key => $val) {
sort($a[$key]);
}
echo '<br><pre>'; print_r($a);
?>[/php]
Ronald :cool:
Jan 22 '07 #2
Thanks for the answer.

In principle, as said, I want to sort it by the highest value, be it in the first, second or third (or twentieth) field.

I wonder I have to change my array-type. Cause until now I used two simple loops to fill my variable:
[PHP]for ($s = 0; $s < $numCountries; $s++)
{
... get country name ...

for ($i = 0; $i < $numYears; $i++)
{
$value[$s][$i] = $row[$year[$i]];
}
}[/PHP]

So, I don't have this kind of associative array. Or should/could I build one?

Thanks for your help!
Jan 22 '07 #3
ronverdonk
4,258 Expert 4TB
I cannot see how you build your array because I am missing the $value, $year and $row variables/definitions. Make a print_r($value) of the array and show it.

Ronald :cool:
Jan 22 '07 #4
Ha, there are so many interesting commands out there, it's incredible. Never heard about the print_r before...

So here is the result:
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => Array ( [0] => 1.1 [1] => 5.9 [2] => ) [1] => Array ( [0] => 4.4 [1] => 4.3 [2] => 5.4 ) [2] => Array ( [0] => 5.9 [1] => 4.9 [2] => 6.3 ) [3] => Array ( [0] => [1] => 3.4 [2] => 11 ) [4] => Array ( [0] => 3.1 [1] => 3.2 [2] => 2.9 ) [5] => Array ( [0] => 10.6 [1] => 8.4 [2] => 4.2 ) [6] => Array ( [0] => 3.6 [1] => 8.2 [2] => 4.2 ) [7] => Array ( [0] => 1 [1] => 2.6 [2] => 2.4 ) )
And this is the graph (although an older version of the currently developed one): Graph
Jan 23 '07 #5
ronverdonk
4,258 Expert 4TB
You actually need 2 sorts here. The 1st one sorts each subarray of 3 entries descending, so the highest is always in entry 0.

The second sort is a multi-sort that sorts the main entry descending on the basis of the subarray entry 0 (highest value in subarray).

The result set shows each entry in the main array, its order based on the highest value anywhere in the sub-array. The result set keeps the key of the main array intact.

Try the following code, assuming the array name $a. The multisort code snippet thanks to remmy.cjb.net in the php documentation at uasort.
[php]<?php
// The default sort order is ascending, and the default sort
// type is strnatcmp.

// function multisort($array[, $key, $order, $type]...)
function multisort($array) {
for($i = 1; $i < func_num_args(); $i += 3) {
$key = func_get_arg($i);

$order = true;
if($i + 1 < func_num_args())
$order = func_get_arg($i + 1);

$type = 0;
if($i + 2 < func_num_args())
$type = func_get_arg($i + 2);

switch($type) {
case 1: // Case insensitive natural.
$t = 'strcasenatcmp($a[' . $key . '], $b[' . $key . '])';
break;
case 2: // Numeric.
$t = '$a[' . $key . '] - $b[' . $key . ']';
break;
case 3: // Case sensitive string.
$t = 'strcmp($a[' . $key . '], $b[' . $key . '])';
break;
case 4: // Case insensitive string.
$t = 'strcasecmp($a[' . $key . '], $b[' . $key . '])';
break;
default: // Case sensitive natural.
$t = 'strnatcmp($a[' . $key . '], $b[' . $key . '])';
break;
}
uasort($array, create_function('$a, $b', 'return ' . ($order ? '' : '-') . '(' . $t . ');'));
}
return $array;
}

echo '<br><pre>PRE-SORT:<br>'; print_r($a);
foreach ($a as $key => $val) {
rsort($a[$key]);
}

echo '<br><pre>SORT 1:<br>'; print_r($a);
// This works like MYSQL 'ORDER BY id DESC, name ASC'
// Note the quoting of string literal keys.
echo '<br><pre>SORT 2:<br>';
print_r(multisort($a, "0", false, 0));
echo('</pre>');
?> [/php]
Ronald :cool:
Jan 23 '07 #6
Thanks for the code.

After playing around with it and especially the impacts it has on my graph production code, it seems that it does not have the results I was expecting.

What it does is actually to sort the (yearly) values of a given element (country). So for:

DE with 12, 23, 28
FR with 8, 28,15
CH with 10, 20, 30


the script will do this:

DE with 28, 23, 12
FR with 28,15, 8
CH with 30, 20, 10


and not

DE with 12, 23, 28
CH with 10, 20, 30
FR with 8, 28,15


or

CH with 10, 20, 30
DE with 12, 23, 28
FR with 8, 28,15


depending on which "column" (=year) I sort.
Jan 24 '07 #7
Thanks for the code.

After playing around with it and especially the impacts it has on my graph production code, it seems that it does not have the results I was expecting.

What it does is actually to sort the (yearly) values of a given element (country). So for:

DE with 12, 23, 28
FR with 8, 28,15
CH with 10, 20, 30


the script will do this:

DE with 28, 23, 12
FR with 28,15, 8
CH with 30, 20, 10

and not
DE with 12, 23, 28
CH with 10, 20, 30
FR with 8, 28,15

or
CH with 10, 20, 30
DE with 12, 23, 28
FR with 8, 28,15


depending on which "column" (=year) I sort.
Actually, I am not anymore too sure about what is being done or not... The print_r shows that everything has been correctly sorted. But the graph just shows a switch in the years... have to digg into this a bit further...
Jan 24 '07 #8
Hmm... I think I know now where the problems lies.

It does - at least if I only use the multisort function in the script above - sort indeed descending for the first value-"colomn":

Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [5] => Array
  4.         (
  5.             [0] => 10.6
  6.             [1] => 8.4
  7.             [2] => 4.2
  8.         )
  9.  
  10.     [2] => Array
  11.         (
  12.             [0] => 5.9
  13.             [1] => 4.9
  14.             [2] => 6.3
  15.         )
  16.  
  17.     [1] => Array
  18.         (
  19.             [0] => 4.4
  20.             [1] => 4.3
  21.             [2] => 5.4
  22.         )
  23.  
  24.     [6] => Array
  25.         (
  26.             [0] => 3.6
  27.             [1] => 8.2
  28.             [2] => 4.2
  29.         )
  30.  
  31.     [4] => Array
  32.         (
  33.             [0] => 3.1
  34.             [1] => 3.2
  35.             [2] => 2.9
  36.         )
  37.  
  38.     [0] => Array
  39.         (
  40.             [0] => 1.1
  41.             [1] => 5.9
  42.             [2] => 
  43.         )
  44.  
  45.     [7] => Array
  46.         (
  47.             [0] => 1
  48.             [1] => 2.6
  49.             [2] => 2.4
  50.         )
  51.  
  52.     [3] => Array
  53.         (
  54.             [0] => 
  55.             [1] => 3.4
  56.             [2] => 11
  57.         )
  58.  
  59. )
But when I allocate this variable to the graphing code, it still counts the first part of the array from 1 to x, even though [3] for example is now on the latest position.

How can I convert the above result into a new variable with the then-current order?
Jan 31 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: J J | last post by:
Hi, I would like some help on the following problem please if anyone can help me. I have two datasets as follows: * Carers * Clients Each client needs X amount of hours to be looked after...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
4
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
6
by: =?Utf-8?B?RGFu?= | last post by:
I am reposting a question from about 3 weeks ago ("sorting capability"). I have an aspx page in which I get the data from a database dynamically, through C# code, by creating a dynamic table...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.