Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 814251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:29:23+00:00 2026-05-15T01:29:23+00:00

Somebody must have come up with a solution for this by now. We are

  • 0

Somebody must have come up with a solution for this by now. We are using PHP 5.2. (Don’t ask me why.) I wrote a PHP class to display a recordset as an HTML table/datagrid, and I wish to expand it so that we can sort the datagrid by whichever column the user selects. In the below example data, we may need to sort the recordset array by Name, Shirt, Assign, or Age fields. I will take care of the display part, I just need help with sorting the data array.

As usual, I query a database to get a result, iterate throught he result, and put the records into an assciateiave array. So, we end up with an array of arrays. (See below.) I need to be able to sort by any column in the dataset. However, I will not know the column names at design time, nor will I know if the colums will be string or numeric values.

I have seen a ton of solutions to this, but I have not seen a GOOD and GENERIC solution Can somebody please suggest a way that I can sort the recordset array that is GENERIC, and will work on any recordset? Again, I will not know the fields names or datatypes at design time. The array presented below is ONLY an example.

UPDATE: Yes, I would love to have the database do the sorting, but that is just not going to happen. The queries that we are running are very complex. (I am not really querying a table of Star Trek characters.) They include joins, limits, and complex WHERE clauses. Writing a function to pick apart the SQL statement to add an ORDER BY is really not an option. Besides, sometimes we already have the array that is a result of the query, rather than the ability to run a new query.

Array
(
    [0] => Array
        (
            [name] => Kirk
            [shrit] => Gold
            [assign] => Bridge
        )

    [1] => Array
        (
            [name] => Spock
            [shrit] => Blue
            [assign] => Bridge
        )

    [2] => Array
        (
            [name] => Uhura
            [shrit] => Red
            [assign] => Bridge
        )

    [3] => Array
        (
            [name] => Scotty
            [shrit] => Red
            [assign] => Engineering
        )

    [4] => Array
        (
            [name] => McCoy
            [shrit] => Blue
            [assign] => Sick Bay
        )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:29:24+00:00Added an answer on May 15, 2026 at 1:29 am

    Well, first off, you should be sorting in the database (it’s more efficient). If not

    In php 5.3+:

    function getSortCommand($field, $sortfunc) {
        return function($var1, $var2) use ($field, $sortfunc) {
            return $sortfunc($var1[$field], $var2[$field]);
        }
    }
    

    in php <= 5.2, you’ll need to create a pure function:

    function getSortCommand($field, $sortfunc) {
        return create_function('$var1, $var2', 'return '.$sortfunc.'($var1["'.$field.'"], $var2["'.$field.'"]);');
    }
    

    Usage for both:

    $array = usort($array, getSortCommand('name', 'strnatcasecmp'));
    

    To adjust the field, you just change the $field parameter. To change the sort function, just change the $sortfunc parameter…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does somebody see something wrong with this? I have an UIAltertView but get an
Could somebody please name a few. I could given time, but this is for
Members at work have come across a C# project posted on Google code that
So I'm having a head against the wall moment and hoping somebody can come
Which is better to use in PHP, a 2D array or a class? I've
I have this task: realize Grails application with ExtJS. And one part of this
Somebody please tell me it is possible to recover Visual Studio source after VS
Can somebody point me to a resource that explains how to go about having
Could somebody please do a rundown of how to programmatically encrypt a config-file in
Can somebody suggest a good free tool for analyzing .Net memory dumps other than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.