Solved

Sorting an array

Posted on 2004-08-30
1
236 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 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now