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

  • Home
  • SEARCH
  • 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 732391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:08:12+00:00 2026-05-14T07:08:12+00:00

I have an array that I would like to sort using a date field

  • 0

I have an array that I would like to sort using a date field from a MySQL database.

Here is a sample of the array which is named news in my class:

[48] => Array
    (
        [id] => 14
        [type] => 3
        [updated] => 2010-04-17 13:54:42
    )

[49] => Array
    (
        [id] => 15
        [type] => 3
        [updated] => 2010-04-17 13:57:21
    )

I want to sort by the updated field.

I have some code I have started but am unsure how to complete it and get it working.

class ProcessClass {
  // ....
  function sortNews($x)
  {
    usort($this->news, array("ProcessClass", "cmp")); //correct sort type?
  }

  function cmp($a, $b)
  {
    //  missing code
  }

Can anyone help??

  • 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-14T07:08:13+00:00Added an answer on May 14, 2026 at 7:08 am

    In most cases, will be easier to add ORDER BY updated to the end of the SQL query, however if your data is coming from multiple sources and you need to resort in PHP, you can use usort() to sort an array based on a user defined function. To use a class function for comparison, the function MUST be static. It can simply compare the updated value in the array using the function strcmp() which happens to return exactly what you need to sort by a MySQL formatted date:

    class ProcessClass {
      // this must be static
      static function sort_by_updated($a, $b)
      {
        return strcmp($a['updated'], $b['updated']);
      }
      
      function sortNews()
      {
        usort($this->news, array("ProcessClass", "sort_by_updated")); 
      }
    

    If you want to reverse sort order, simply swap the comparison params: return strcmp($b['updated'], $a['updated'])

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer /** * This is a fast code to get the… May 15, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer The place I work had similar problems, but perhaps not… May 15, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer getuid or geteuid would be the obvious choices. getuid checks… May 15, 2026 at 5:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.