Welcome Guest, Not a member yet? Register   Sign In
sorting database results by multiple keys
#1

[eluser]Bogdan Tanase[/eluser]
Hi,

I'm trying to sort some database results (which are modified after SQL query) by more than one field. something like ORDER BY clause in SQL.

I've got to the point of using usort and a callback function for sorting by one field, but I can't seem to find a way of sorting by multiple fields.

Any ideas, or a link towards a more advanced sorting algorithm for arrays?

Thanks!
#2

[eluser]ray73864[/eluser]
the general rule is if you can do it in the database then you should do it in the database.

the main reasoning is because the database is quicker at doing it than any piece of code you create to do it.

so as jeremy said, pass the sort information to the query and do the ORDER BY that way.
#3

[eluser]Bogdan Tanase[/eluser]
Hi,

thanks for the responses but ...

the table is generated using a DB query, but after that, new fields are added, values are calculated using data from different sources; basically i'm creating a completly new result set, impossible to get directly from SQL, so sorting in query is not an option.

Besides, I'm talking about tables with 100-200 records (the result set) so, I don't think an array sorting will take too long. It should be done in memory, so I expect it to be fast!

I thought of creating a temporary mysql table with the calculated results and then sort with SQL that table and display it, but it doesn't sound "right" to me...




Theme © iAndrew 2016 - Forum software by © MyBB