Solved

Sorting an array

Posted on 2004-08-30
1
282 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

696 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