Solved

Sorting an array

Posted on 2004-08-30
1
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 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