473,387 Members | 1,771 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,387 software developers and data experts.

Sort question - make elements with = sign first


Folks,

I know I could do this with a foreach loop but it looks dirty. I'm
wondering if I can do this via array_walk() or asort() and would appreciate
some help..

I have an array - an example follows

$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

The elements can be written up in any order, thus the last element "ref=1"
could have been anywhere in the array, its just for this example that I have
placed it as the last element.

I need to read these elements, but I need to give precedence to first
reading the elements that contain an equals sign before the others... It
does *not* have to be alphabetically sorted - so the following result would
be acceptable:

$tmpArray[0]="ref=1";
$tmpArray[1]="one";
$tmpArray[2]="two";
$tmpArray[3]="three";
$tmpArray[4]="apples";

Can anyone suggest/tell me a method that might be environmentally friendly
to do this? I think array_walk might help but I've never worked out how to
use it...

Thanks
randelld
Jul 16 '05 #1
5 2402
On Mon, 08 Sep 2003 21:03:41 GMT, "Randell D."
<yo**************************@yahoo.com> wrote:

Folks,

I know I could do this with a foreach loop but it looks dirty. I'm
wondering if I can do this via array_walk() or asort() and would appreciate
some help..

I have an array - an example follows

$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

The elements can be written up in any order, thus the last element "ref=1"
could have been anywhere in the array, its just for this example that I have
placed it as the last element.

I need to read these elements, but I need to give precedence to first
reading the elements that contain an equals sign before the others... It
does *not* have to be alphabetically sorted - so the following result would
be acceptable:

$tmpArray[0]="ref=1";
$tmpArray[1]="one";
$tmpArray[2]="two";
$tmpArray[3]="three";
$tmpArray[4]="apples";

Can anyone suggest/tell me a method that might be environmentally friendly
to do this? I think array_walk might help but I've never worked out how to
use it...


<pre>
<?php
$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

function equalsSort($a,$b) {
if (strstr($a,'=')) return -1;
if (strstr($b,'=')) return 1;
return strcmp($a,$b);
}

usort($tmpArray, 'equalsSort');

var_dump($tmpArray);
?>
</pre>

array(5) {
[0]=>
string(5) "ref=1"
[1]=>
string(6) "apples"
[2]=>
string(3) "one"
[3]=>
string(5) "three"
[4]=>
string(3) "two"
}

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2
Randell D. <yo**************************@yahoo.com> wrote:
Folks,

I know I could do this with a foreach loop but it looks dirty. I'm
wondering if I can do this via array_walk() or asort() and would appreciate
some help..
You'll need usort.

http://nl.php.net/manual/en/function.usort.php

<?php

$list = array('one', 'two', '=three', 'four', '=five');
print_array ($list);
usort($list, "is_prec");
print_array ($list);

function is_prec ($a, $b) {
if (substr($a,0,1) == '=' )
{
return -1;
}
elseif (substr($a,0,1) <> '=')
{
return 1;
}
else return 0;
}

function print_array($ar)
{
print "<pre>"; print_r($ar);print"</pre>";

}
?>

I have an array - an example follows

$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

The elements can be written up in any order, thus the last element "ref=1"
could have been anywhere in the array, its just for this example that I have
placed it as the last element.

I need to read these elements, but I need to give precedence to first
reading the elements that contain an equals sign before the others... It
does *not* have to be alphabetically sorted - so the following result would
be acceptable:

$tmpArray[0]="ref=1";
$tmpArray[1]="one";
$tmpArray[2]="two";
$tmpArray[3]="three";
$tmpArray[4]="apples";

Can anyone suggest/tell me a method that might be environmentally friendly
to do this? I think array_walk might help but I've never worked out how to
use it...

Thanks
randelld


Goodluck.

--
Maarten van der Peet
Jul 16 '05 #3

"Maarten van der Peet" <Ma******************@haalditvetweg.xs4all.nl> wrote
in message
news:1g0zrl6.1j3vurcbn178N%Ma******************@ha alditvetweg.xs4all.nl...
Randell D. <yo**************************@yahoo.com> wrote:
Folks,

I know I could do this with a foreach loop but it looks dirty. I'm
wondering if I can do this via array_walk() or asort() and would appreciate some help..


You'll need usort.

http://nl.php.net/manual/en/function.usort.php

<?php

$list = array('one', 'two', '=three', 'four', '=five');
print_array ($list);
usort($list, "is_prec");
print_array ($list);

function is_prec ($a, $b) {
if (substr($a,0,1) == '=' )
{
return -1;
}
elseif (substr($a,0,1) <> '=')
{
return 1;
}
else return 0;
}

function print_array($ar)
{
print "<pre>"; print_r($ar);print"</pre>";

}
?>

I have an array - an example follows

$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

The elements can be written up in any order, thus the last element "ref=1" could have been anywhere in the array, its just for this example that I have placed it as the last element.

I need to read these elements, but I need to give precedence to first
reading the elements that contain an equals sign before the others... It
does *not* have to be alphabetically sorted - so the following result would be acceptable:

$tmpArray[0]="ref=1";
$tmpArray[1]="one";
$tmpArray[2]="two";
$tmpArray[3]="three";
$tmpArray[4]="apples";

Can anyone suggest/tell me a method that might be environmentally friendly to do this? I think array_walk might help but I've never worked out how to use it...

Thanks
randelld


Goodluck.

--
Maarten van der Peet


I don't think you read my entire post properly... I do not have any element
that begins with an = sign, instead, I have *some* elements that *have* an
equals sign somewhere. The array you created in your example is not the
same as the array in my question.

Thanks for trying though.
Jul 16 '05 #4
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:uv********************************@4ax.com...
On Mon, 08 Sep 2003 21:03:41 GMT, "Randell D."
<yo**************************@yahoo.com> wrote:

Folks,

I know I could do this with a foreach loop but it looks dirty. I'm
wondering if I can do this via array_walk() or asort() and would appreciatesome help..

I have an array - an example follows

$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

The elements can be written up in any order, thus the last element "ref=1"could have been anywhere in the array, its just for this example that I haveplaced it as the last element.

I need to read these elements, but I need to give precedence to first
reading the elements that contain an equals sign before the others... It
does *not* have to be alphabetically sorted - so the following result wouldbe acceptable:

$tmpArray[0]="ref=1";
$tmpArray[1]="one";
$tmpArray[2]="two";
$tmpArray[3]="three";
$tmpArray[4]="apples";

Can anyone suggest/tell me a method that might be environmentally friendlyto do this? I think array_walk might help but I've never worked out how touse it...


<pre>
<?php
$tmpArray[0]="one";
$tmpArray[1]="two";
$tmpArray[2]="three";
$tmpArray[3]="apples";
$tmpArray[4]="ref=1";

function equalsSort($a,$b) {
if (strstr($a,'=')) return -1;
if (strstr($b,'=')) return 1;
return strcmp($a,$b);
}

usort($tmpArray, 'equalsSort');

var_dump($tmpArray);
?>
</pre>

array(5) {
[0]=>
string(5) "ref=1"
[1]=>
string(6) "apples"
[2]=>
string(3) "one"
[3]=>
string(5) "three"
[4]=>
string(3) "two"
}

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)


Cheers - that works perfectly... and looks tidy too...
Jul 16 '05 #5
Randell D. <yo**************************@yahoo.com> wrote:

<snip>
I don't think you read my entire post properly... I do not have any element
that begins with an = sign, instead, I have *some* elements that *have* an
equals sign somewhere. The array you created in your example is not the
same as the array in my question.
Yes you're right. I must have had a black out. :-)

But the understanding and using of 'usort' is still valid.
Thanks for trying though.


You're welkom.

--
Maarten van der Peet
Jul 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
15
by: KraftDiner | last post by:
I have two lists. I want to sort by a value in the first list and have the second list sorted as well... Any suggestions on how I should/could do this?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
5
by: jason735 | last post by:
Hi, I've got the following problem. I have to sort x*y elements which are in one file. I can use only an array for x elements and floor tmp files which can be read only forward. Thanks for...
75
by: At_sea_with_C | last post by:
Hello all, I have written an ascending sort routine for floats. This seems to do the job, but for elements over 10,000, it gets awfully slow. A lot of useless comparisions with previously sorted...
0
Ganon11
by: Ganon11 | last post by:
So far, the Articles sections are filled with slow sorting algorithms. Bubble Sort and Selection Sort, while very easy to comprehend, are relatively slow algorithms at Θ(n^2) running time. Here, I...
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: 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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.