Jump to content

Sorting In PHP vs. DB

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
BASHERS33

BASHERS33

    Programmer

  • Members
  • PipPipPipPip
  • 198 posts
A programmer kept insisting to em it's best to sort in the mysql query than to use such things as ksort, krsort, etc... in the php. Yet I see in Invision's new software they contiunue to do some sorting with php rather than the query.

Any thoughts? I got rid of all my php sorting and then see others do it and wonder if I just wasted time.

I had some other question and forgot. Blah.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Databases are best for information handling, even as in sorting, I would not say anything else. it's part of what it is made to do. I'd avoid sortings in php if I could, but at times, maybe the database won't do, maybe you want to modify your data and then sort, or similar, and therefore need to sort by php...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Yes sorting in a DB is extremly handy, if you are just modifying a number you can even do that in sql. I remember making a ranking algorithm that would use things like hits, creation date, ratings, and a few other things, using sql I got it to have a ranking value and sort by that.

Even if you do not consider speed tho, its really sloppy to add extra code to sort things.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would do the sorting in the DB. Some DB's will do an implicit sort based on the primary key if you don't explicitly sort, so you can end up wasting computing resources by effectively "double sorting", once in the DB, and then once in PHP.

Big projects don't always do a good job of following best-practices in all areas.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
BASHERS33

BASHERS33

    Programmer

  • Members
  • PipPipPipPip
  • 198 posts
ok, thanks. I have it currently sorting in the DB in almost every case. I think there was one case where it just needed to be done in the php due to some data modifying, but that's it... one case.

In the IPB forum software I saw a place where he query was run and then immediately it was sorted. So not sure why they did that. There was no manipulation or anything and it was a simple krsort so why they didn;'t just order by the key descending I don't know.