SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting Arrays vs SELECT ORDER BY

    SO i have a table of information. the information is from an array $camps that i got from a mySql db.

    I would like to give the user the ability to click on each separate colum title in the table to sort it by the specific column.

    I have looked at sort(), uasort(), asort(), etc. but not sure exactly how to use them for multidemensional arrays.

    since the array is comming from another php page/aplication, i thought it would be easier to resort the array rather than do a new SELECT query every time they want to resort the data.

    which is the better and more efficient way to resort?

  2. #2
    SitePoint Enthusiast Bobbi's Avatar
    Join Date
    Mar 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From my experience, I'd certainly go with mySQL (ORDER BY) in that case. It's a highly optimized database language with features such as query caching etc., which should give you way better performance than the sort functions.

    Bobbi

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Definitely use the database.

    You either have to query again or store the array in the Session anyway, which is arguably less scalable, particularly on large data sets.

    Queries are pretty cheap in the majority of cases and it makes sense to leverage the strength of the database in returning data in the correct order -> this is exactly what databases are optimised to do.

  4. #4
    SitePoint Addict battra's Avatar
    Join Date
    Oct 2004
    Location
    Asylum
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really don't want to run the query more than once, you can use JavaScript to re-order the table. But IMO it's still much better to use ORDER BY.

  5. #5
    SitePoint Zealot swiftone's Avatar
    Join Date
    Mar 2005
    Location
    Sunny Florida
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Database is much faster, especially if the data is of significant size to sort.
    Happy Coding,
    Swiftone

    Unique Web Sites


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •