Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting top five from array

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
I have a function that gets the number of submitted tickets for each user. How do I make this get the top five submitters (and their respective ticket totals) and lump the rest of the count into "other." This data is being pulled from a database. thanks!

Code:
var user_burt_count = [];
for ( var i=0; i < data.length; i++ ) {            
                var submitter = data[i].sub_by;
                if( !( submitter in user_burt_count ) ) {
                                user_burt_count[submitter] = 1;
                }
                else {
                   user_burt_count[submitter]++;
                }
}


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Hi

Well just sort them, then sum all starting at 5[sup]th[/sup] item.

In this code you will have the resulting top 5 + 1 elements in object top :
JavaScript:
[b]var[/b] order = Object.keys(user_burt_count).sort([b]function[/b](a, b) {
  [b]return[/b] user_burt_count[ignore][b][/ignore] - user_burt_count[a]
})

[b]var[/b] top = {}, other = 0
[b]for[/b] ([b]var[/b] i = 0, l = order.length; i < l; i++)
  [b]if[/b] (i < 5)
    top[order[ignore][i][/ignore]] = user_burt_count[order[ignore][i][/ignore]]
  [b]else[/b]
    other += user_burt_count[order[ignore][i][/ignore]]

[b]if[/b] (other)
  top[[green][i]'other'[/i][/green]] = other

See it in action on .

If I misunderstood you, please clarify the requirements.


Feherke.
feherke.ga
 
perfect, thanks!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top