[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sorting an array

Posted on 2004-08-30
1
Medium Priority
?
293 Views
Last Modified: 2006-11-17
Hello.

I to create an array, I place this line in three different mySQL table queries:

------------------------------------------------------
$list[] = array("tstamp" => $row[TSTAMP], "title" => $row[TITLE], "notes" => $row[notes]);
------------------------------------------------------

Now I have an array with 40 items, and I want to sort them in ascending order by tstamp (an integer).

I looked at the PHP manual pages, but got confused by the many sorting functions:
arsort(), asort(), ksort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort().

What is the easiest way to sort this array by tstamp?
0
Comment
Question by:hankknight
1 Comment
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 11935769
The following code should do what you want

// define you sorting function
function mycmp($a,$b) {
    if ($a['tstamp'] == $b['tstamp']) {
        return 0;
    }
    return $a['tstamp'] > $b['tstamp'] ? 1 : -1;
}

usort($list, 'mycmp');
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question