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 9309719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:56:12+00:00 2026-06-19T00:56:12+00:00

I have a poll on my website. For it I put the results into

  • 0

I have a poll on my website. For it I put the results into an array such as this:

[Answer1] => "0",
[Answer2] => "1",
[Answer3] => "0",
[Answer4] => "0"

The choice is the key and the number of votes is the value. Essentially when the results show I want to order by the number of votes and then also keep the same sort. So if no votes it would show in the order:

Answer1
Answer2
Answer3
Answer4

But, with “Answer2” having 1 vote it would show:

Answer2
Answer1
Answer3
Answer4

I have tried creating a custom usort function but am having no luck.
Can anybody help me with the logic how to do it please (i can do the PHP myself)?

  • 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-06-19T00:56:14+00:00Added an answer on June 19, 2026 at 12:56 am

    As you are using an associative array what you are looking for is called a stable sort.
    PHP’s standard array sorting functions use an unstable implementation of quicksort so as you have found they will not help you here.

    You could accomplish this though using array_multisort. By sorting by BOTH the value and the key.

    With your data structure you currently have you could not do this with any of the u*sort functions BUT if you modified the data structure and added another dimension so that it looked like the following then you could:

    array( 
      array( 
        'answer' => 1, 
        'votes' => 0, 
      ), 
      array( 
        'answer' => 2, 
        'votes' => 1, 
      ), 
    ) 
    

    So essentially the comparison function would just need to take into account both sub-indexes.

    hope that helps

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

Sidebar

Related Questions

I have something like this in my code: worker.setObject(queue.poll()); I want a queue that
On my website, I have a poll. It's cookie based poll. If a user
I downloaded the PHP voting script from this website: I have installed this and
I have a website that has a voting poll module. A poll controller accepts
I have created a DNN module for a Poll. This module is created as
I have a poll system and I want answers for this poll to be
I have a website where I need to poll a webpage in every minute
I'm new with django. I have this poll app, and I want to limit
Say I have a database called Poll and have these two table structures. poll
I have a php/mysql poll that I'm trying to modify. It sets a unique

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.