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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:31:33+00:00 2026-06-13T21:31:33+00:00

My first question on this topic was answered and it helped me move forward

  • 0

My first question on this topic was answered and it helped me move forward so I apologize if this seems like it’s duplicate. I’ve moved forward from the 1st example and am now trying to Rank elements in a nested array based on values at postion (1) in each element of the nested array. The following works great with a number array.

function mySorting(a, b) {
    return a == b ? 0 : (b < a ? -1 : 1)
}

var myArray = [28, 92, 12, 12, 2];
myArray.sort(mySorting);

var ranks = $.grep(myArray, function(item, idx) {
    return item != myArray[idx - 1];
}).reverse();



$.each(myArray, function(idx, item) {
    var rank= $.inArray( item, ranks)+1;
    $('body').append('Rank of '+item+ ' is '+ rank+'<br>')

})​

My problem is when I change the array to:

var myArray = [["textA",28], ["textB",92], ["textC",12], ["textD",12], ["textE",2]];

my sorting function no longer works. Can anyone help me out or point me in the right direction? I would like to sort “myArray” based of the number value in each nested element.

Thanks much once again in advance.

  • 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-13T21:31:35+00:00Added an answer on June 13, 2026 at 9:31 pm

    The arguments passed to the sorting function are whatever is contained in the array to be sorted. Your new array contains two-element arrays, so that’s what the sorting function will receive when you sort the array.

    You say you want to sort the two-element arrays according to their second element. To do that, you should change your sorting function to look like this:

    function mySorting(a, b) {
        return a[1] == b[1] ? 0 : (b[1] < a[1] ? -1 : 1)
    }
    

    Edit: If you want to assign the same rank to all arrays with the same second element, you’ll also need to modify the rest of your code to only consider the second element when comparing arrays. Actually, it would probably be easiest to keep the sorting as it was, but just throw away everything but the second element before ranking the arrays:

    function toSortKey(item) {
        return item[1];
    }
    
    function mySorting(a, b) {
        return a == b ? 0 : ( b < a ? -1 : 1 );
    }
    
    var myArray = [["textA",28], ["textB",92], ["textC",12], ["textD",12], ["textE",2]];
    
    var sortKeys = $.map(myArray, toSortKey);
    sortKeys.sort(mySorting);
    
    var ranks = $.grep(sortKeys, function(item, idx) {
        return item != sortKeys[idx - 1];
    }).reverse();
    
    $.each(myArray, function(idx, item) {
        var rank = $.inArray( toSortKey(item), ranks ) + 1;
        $('body').append('Rank of ' + item + ' is ' + rank + '<br>')
    })​;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question in here, and I would like to ask if
While I have made a topic like this in the past, this is from
I have two STUFF questions for this topic. First question is STUFF function in
First of all, sorry to post a question like this when so many other
Hey this is my first question on this website. Im creating a webpage for
This is my first question in this forum, so i hope to be clear:
nobody loved my first question about this: Creating Entity Framework objects with Unity for
This is my first question to this forum. I am trying to make this
this is my first question on here. The question is simple - # this
Hello this is may first question and I have found so far the following

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.