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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:07:03+00:00 2026-06-04T21:07:03+00:00

Okay, this one is pretty complicated… lets say I have an array called $data

  • 0

Okay, this one is pretty complicated… lets say I have an array called $data

Array
(
    [9e5b0d6d-711c-4cd0-8697-634a5c640066] => Array
        (
            [player] => 9e5b0d6d-711c-4cd0-8697-634a5c640066
            [wins] => 1
            [loss] => 6
            [winsR] => 2
            [lossR] => 12
        )

    [a8f7fb4c-afab-4ec0-9202-b87fa2641110] => Array
        (
            [player] => a8f7fb4c-afab-4ec0-9202-b87fa2641110
            [wins] => 1
            [loss] => 6
            [winsR] => 2
            [lossR] => 12
        )

    [96ed2f95-08bd-42f8-bb57-9df09b66109d] => Array
        (
            [player] => 96ed2f95-08bd-42f8-bb57-9df09b66109d
            [wins] => 2
            [loss] => 5
            [winsR] => 4
            [lossR] => 10
        )

    [2a4e1a3e-cde4-4c9b-947e-67a4d333f0f4] => Array
        (
            [player] => 2a4e1a3e-cde4-4c9b-947e-67a4d333f0f4
            [wins] => 3
            [loss] => 4
            [winsR] => 6
            [lossR] => 8
        )

    [08c23d63-69a9-4147-b40f-64b241cfbb4f] => Array
        (
            [player] => 08c23d63-69a9-4147-b40f-64b241cfbb4f
            [wins] => 4
            [loss] => 3
            [winsR] => 8
            [lossR] => 6
        )

    [777e8f3e-caac-41fb-975e-a410e42f7114] => Array
        (
            [player] => 777e8f3e-caac-41fb-975e-a410e42f7114
            [wins] => 5
            [loss] => 2
            [winsR] => 11
            [lossR] => 4
        )

    [9cd5256f-6d3a-4bd5-aeed-b6904bcbf048] => Array
        (
            [player] => 9cd5256f-6d3a-4bd5-aeed-b6904bcbf048
            [wins] => 6
            [loss] => 1
            [winsR] => 12
            [lossR] => 3
        )

    [797c95df-c334-4328-a8eb-4ba5e690af4d] => Array
        (
            [player] => 797c95df-c334-4328-a8eb-4ba5e690af4d
            [wins] => 6
            [loss] => 1
            [winsR] => 12
            [lossR] => 2
        )
)

As you can see, I have a series of data, and each element has it’s own nested array. I already know I can sort the array using a single nested value using

usort($data, create_function('$a, $b', 'return $b["wins"] - $a["wins"];')); 

However, this is not enough.

The problem is, sorting with the above function doesn’t sort deep enough for me. All it does is sort based on the following:

  • [‘wins’] DESCENDING (highest wins sorted first)

I would like to sort using FOUR nested values.

  • [‘wins’] DESCENDING (highest wins sorted first)
  • [‘loss’] ASCENDING (lowest loss sorted second)
  • [‘winsR’] DESCENDING (highest winsR sorted third)
  • [‘lossR’] ASCENDING (lowest lossR sorted fourth)

I know this is pretty complicated, so I’m coming here to ask for help. What would be the best and most efficient way to do this?

  • 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-04T21:07:05+00:00Added an answer on June 4, 2026 at 9:07 pm

    What you’re looking for is the function usort().

    There, you can supply your own comparator that can compare two elements and apply the rules you wish to use.

    You were on the right track, but your comparator function can be much more elaborate:

    function compare_elements($l, $r) {
        if ($l['wins'] > $r['wins']) {
            return -1;
        } else if ($l['wins'] < $r['wins']) {
            return 1;
        }
    
        // Drop to second level; it will have exited by here
        // if the tie could be broken by the first rule.
        // You really could've put this in an else clause, but I
        // wanted to avoid excessive nesting.
        if ($l['loss'] > $r['loss']) {
            return 1;
        } else if ($l['loss'] < $r['loss']) {
            return -1;
        }
    
        // And so on until the 4th level    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I need help. I'm usually pretty good at SQL queries but this one
Okay, this one is pretty obvious to everyone who use Django and frequently asked
Okay, have a bit of a tricky one (for me anyway, i'm pretty rubbish
Okay this one's pretty straightforward. I'm creating a checkbox in my Page.xaml.cs file, and
Okay, so I have this page, which has one Ajax:AsyncFileUploader also some other ajax
Okay, here's what I have... On one server, a WCF hosted in IIS. This
Okay, I've been banging my head against the wall with this one. I have
Okay, This one is pretty simmilar to my last one, but I don't get
Okay this one has me stumped.. mainly because i have been working on this
Okay this is one of those situations where I have concluded that I am

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.