468,545 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

How to sort an array, leaving one or more columns unsorted?

Greetings. I'm quite new to PHP and have very little knowledge of
programming in general, so I may be missing something obvious, but I
can't find a solution to the following difficulty in either the PHP
manual or Google.

I have a tab-delimited text file containing numerous lines of data,
which is searched for matches to a query from an HTML form. I want to
be able to sort the resulting two-dimensional array of matches by one
column only, leaving the other columns unsorted. For example, if three
lines match "bar0",

foo02 bar01 foobar foobar
foo03 bar02 foobar foobar
foo01 bar01 foobar foobar

should become

foo02 bar01 foobar foobar
foo01 bar01 foobar foobar
foo03 bar02 foobar foobar

not

foo01 bar01 foobar foobar
foo02 bar01 foobar foobar
foo03 bar02 foobar foobar

The following method works for sorting on the second column, but seems
both inelegant and inflexible (especially if I want to sort on the
third or fourth column instead:

#v+

<?php
$data = file("filename.txt");
$sortkey = 0;
foreach($data as $line) {
// if the line matches a regex, do this:
$temp = explode("\t", rtrim($line,"\n"));
$newdata[]= array($temp[1], $sortkey, $temp[0], $temp[2], $temp[3]);
$sortkey++;
}
if(sizeof($newdata) > 0) {
sort($newdata);
foreach($newdata as $line) {
echo $line[2]."\t".$line[0]."\t".$line[3]."\t".$line[4]."\n";
}
}
?>

#v-

Is there a less clumsy way of doing this kind of thing without having
to use a "real" database? I've tried all the inbuilt array-sorting
functions, without any success so far.

PJR :-)
--
Nemo hibericam exspectat inquisitionem.

alt.usenet.kooks award-winners and FAQ:
<http://www.insurgent.org/~kook-faq/>

Nov 24 '05 #1
3 1597
Peter J Ross wrote:
Is there a less clumsy way of doing this kind of thing without having
to use a "real" database? I've tried all the inbuilt array-sorting
functions, without any success so far.


Untested, but might try something like what I and subsequently rojaro
discussed at http://php.net/asort:

<?php
$data = file("filename.txt");
$sortCol = 1; // sort on second column
$aLines = []; // original data
$aSorted = []; // this will contain the resulting, sorted data
foreach($data as $line) {
// if the line matches a regex, do this:
$aLine[] = $line;
$temp = explode("\t", rtrim($line,"\n"));
$aSorted[] = $temp[$sortCol]; }
if ($aSorted) {
// sort on selected column retaining line associations
asort($aSorted);
// now replace selected column val with entire line
foreach ($aSorted as $idx => &$val) $val = $aLine[$idx];
// normalize the keys to be 0..sizeof($aSorted)-1
$aSorted = array_merge($aSorted);
// show the sorted lines
foreach ($aSorted as $line) echo "$line\n";
}
?>

Csaba Gabor from Vienna

Nov 26 '05 #2
On 25 Nov 2005 17:24:22 -0800, Csaba Gabor <Cs***@z6.com> wrote in
comp.lang.php:
Peter J Ross wrote:
Is there a less clumsy way of doing this kind of thing without having
to use a "real" database? I've tried all the inbuilt array-sorting
functions, without any success so far.


Untested, but might try something like what I and subsequently rojaro
discussed at http://php.net/asort:


It's certainly tidier than my kludge. Many thanks. If it doesn't work
after I've tested it I'll let you know.

PJR :-)
--
Nemo hibericam exspectat inquisitionem.

alt.usenet.kooks award-winners and FAQ:
<http://www.insurgent.org/~kook-faq/>

Nov 26 '05 #3
Peter J Ross wrote:
be able to sort the resulting two-dimensional array of matches by one
column only, leaving the other columns unsorted.


Does the ordering of the other columns not matter and do you just want to
avoid doing extra work, or does it specifically need to be left exactly
the way it went in?

If the former, just use usort with your own comparison function:

$col = 2; //set to right column number to sort on
function cmp($a, $b) {
global $col;
return strcmp($a[$col], $b[$col]); //or a numerical comparison perhaps
}
usort($myTwoDimensionalArray, 'cmp');

If the latter, you'll have to write your own (relatively) low-level
sorting routine because the order of identical elements is not defined for
built-in sorting routines, ie. you can't rely on them to just leave those
elements in their existing order. (unless stated thusly in the manual..).

--
E. Dronkert
Nov 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by yeti349 | last post: by
4 posts views Thread by christiang | last post: by
1 post views Thread by veg_all | last post: by
1 post views Thread by millw0rm | last post: by
4 posts views Thread by Santosh Nayak | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.