You are here: Re: How to sort an array, leaving one or more columns unsorted? « PHP Programming Language « IT news, forums, messages
Re: How to sort an array, leaving one or more columns unsorted?

Posted by Ewoud Dronkert on 11/26/05 22:34

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация