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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:57:02+00:00 2026-06-17T06:57:02+00:00

Possible Duplicate: Sorting JavaScript Object by property value I want to get the top

  • 0

Possible Duplicate:
Sorting JavaScript Object by property value

I want to get the top results for some value within my JSON. Easier explained with the example:

var jsonData = {
  bom: [
        {
            "Component":"Some Thing",
            "Version":"Version ABC",
            "License":"License ABC",
        },
        {
            "Component":"Another Thing",
            "Version":"Version XYZ",
            "License":"License ABC",
        }, 
        etc ....
       ]
}

So my goal is to determine that “License ABC” or another has X number of occurrences and then I want to be able sort those key:val pairs to insert into into the DOM as “The top X most popular licenses are:

  • License ABC – 100
  • License XYZ – 70
  • License 123 – 25

Right now I have this:

var countResults = function() {
    var fileLicenses = [];

    for ( var i = 0, arrLen = jsonData.files.length; i < arrLen; ++i ) {
        fileLicenses.push(jsonData.files[i]["License"]);
    }

    keyCount = {};
    for(i = 0; i < fileLicenses.length; ++i) {
        if(!keyCount[fileLicenses[i]]) {
            keyCount[fileLicenses[i]] = 0;
        }

        ++keyCount[fileLicenses[i]];
    }

    console.log( keyCount );
}();

Which get’s me most of what I want, an object with key : values

{
    thisKey : 78,
    thatKey :125,
    another key : 200,
    another key : 272,
    another key : 45,
    etc ...
}

But I don’t know how to do anything with that. I just need to sort the numeric right column and have the associated keys stay along for the ride. Thoughts? Thank you!

  • 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-17T06:57:04+00:00Added an answer on June 17, 2026 at 6:57 am

    You can’t sort an object by it’s values. What you can do is to transform it to an array of objects and sort that instead. Something like:

    var rank = function(items, prop) {
    
      //declare a key->count table
      var results = {}
    
      //loop through all the items we were given to rank
      for(var i=0;len=items.length;i<len;i++) {
    
        //get the requested property value (example: License)
        var value = items[i][prop];
    
        //increment counter for this value (starting at 1)
        var count = (results[value] || 0) + 1;
        results[value] = count;
      }
    
      var ranked = []
    
      //loop through all the keys in the results object
      for(var key in results) {
    
        //here we check that the results object *actually* has
        //the key. because of prototypal inheritance in javascript there's
        //a chance that someone has modified the Object class prototype
        //with some extra properties. We don't want to include them in the
        //ranking, so we check the object has it's *own* property.
        if(results.hasOwnProperty(key)) {
    
          //add an object that looks like {value:"License ABC", count: 2} 
          //to the output array
          ranked.push({value:key, count:results[key]}); 
        }
      }
    
      //sort by count descending
      return ranked.sort(function(a, b) { return b.count - a.count; });
    }
    

    Usage:

    var sorted = rank(jsonData.bom, "License");
    var first = sorted[0].value;
    

    /code not tested

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

Sidebar

Related Questions

Possible Duplicate: Sorting JavaScript Object by property value Since I've noticed the browser can't
Possible Duplicate: Sorting objects in an array by a field value in JavaScript How
Possible Duplicate: Sorting objects in an array by a field value in JavaScript var
Possible Duplicate: Sorting an array based on its value I want to sort an
Possible Duplicate: Sorting a hash in Ruby by its value first then its key.
Possible Duplicate: Sorting or Finding Max Value by the second element in a nested
Possible Duplicate: Sorting a tuple that contains tuples I am having some trouble with
Possible Duplicate: jQuery tablesorter - Not sorting column with formatted currency value This http://tablesorter.com/docs/example-meta-parsers.html
Possible Duplicate: How to store an IP in mySQL I want to get the
Possible Duplicate: Sorting an array in descending order in Ruby I want to sort

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.