3

The API provides methods to sort and filter individual questions by score.

In order to calculate the question score (votes sum) totals per User, some grouping and set operations are required.

e.g.

total_score display_name [user_id]
------------------------------------------
1000 John Doe [12]
900 Joe Snow [431]
. . . etc etc

Post your solution to this use case in the language/library syntax of your choice.

2
  • Here you want the user with the most votes for questions they have posted?
    – jjnguy
    Aug 16, 2010 at 21:14
  • @jjn - this use case involves the sum score. The logical difference is that votes on CW questions do not count towards score. The functional difference is nil as both fields are present on the question object. Aug 16, 2010 at 21:51

2 Answers 2

1

C# / Soapi.CS


using Soapi.CS

Context
    .Official
    .StackApps
    .Questions
    .PageCount(0) // all pages, be careful with this
    .PageSize(100)
    .GroupBy(question => question.OwnerId)
    .Select(group => new
    {
        User = group.First().Owner.DisplayName + "[" + group.First().Owner.UserId + "]",
        Score = group.Sum(question => question.Score)
    }).OrderByDescending(item => item.Score)
    .ToList().ForEach(item => Console.WriteLine("{0} - {1}", item.Score, item.User));

Output:

265 - George Edison[18]
262 - code poet[14]
187 - Kevin Montrose[2]
88 - systempuntoout[51]
76 - Soviut[825]
71 - S.Mark[40]
70 - Felix[956]
70 - Farseeker[816]
65 - lfoust[11]
50 - Adam Wright[5]
41 - Dave Swersky[29]
38 - Steffen Opel[377]
38 - carson[33]
38 - Edan Maor[1241]
36 - Franci Penov[13]
32 - Lucas Jones[570]
31 - Bill the Lizard[9]
29 - Dave DeLong[448]
28 - Thomas McDonald[1031]
28 - Mark Rushakoff[1048]
25 - Jedi Master Spooky[192]
25 - Shay Erlichmen[614]
24 - johnwards[638]
24 - swanson[839]
23 - TheHurt[17]
22 - codeka[1290]
22 - Catchwa[700]
21 - Dennis Williamson[171]
20 - radius[295]
20 - SztupY[1265]
17 - jjnguy[39]
17 - Matt S.[30]
17 - Kyle Cronin[42]
17 - ColinD[222]
16 - Yacoby[1049]
16 - Jeff Atwood[4]
16 - Ricky[199]
16 - Jason[19]
15 - Nick Presta[186]
15 - Adam[1395]
15 - eWolf[664]
14 - Joel Potter[8]
13 - Ryan Brunner[1263]
12 - RichH[757]
11 - Igor Zevaka[113]
11 - InfinitiesLoop[1394]
11 - adrianbanks[127]
11 - Koning Baard[451]
10 - nabeelmukhtar[1026]
10 - Martin Plante[28]
9 - phsr[693]
8 - Nathan Reed[680]
8 - Robert Munteanu[358]
8 - Peter Mourfield[424]
8 - Jan Fabry[1303]
7 - conmulligan[449]
7 - Josh Kelley[2287]
6 - balpha[43]
6 - blork[1849]
6 - Jonathan[938]
6 - Michael B.[234]
6 - Kevin[2296]
6 - Peter Mortensen[151]
6 - Martijn Laarman[56]
5 - Soldier.moth[91]
5 - TheKaptain[1232]
5 - fukas78[667]
5 - chenyuejie[2292]
5 - mlaw[2467]
5 - James A. Rosen[140]
5 - Tim Post[52]
5 - Riduidel[2192]
5 - Joe[2418]
5 - Chacha102[23]
5 - mikej[339]
4 - AidenMontgomery[2894]
4 - Nathan Voxland[2442]
4 - Matthew Pelser[138]
4 - Nippysaurus[1221]
4 - bpedro[2222]
4 - Robert Cartaino[431]
4 - jmoy[1978]
4 - iconiK[375]
4 - denny[553]
4 - John Bristowe[70]
4 - Mark Hurd[44]
3 - tonklon[2734]
3 - Matt Culbreth[2829]
3 - magcius[1304]
3 - James Johnson[2517]
3 - Maxim Zaslavsky[32]
3 - daltojr[1009]
3 - Bialecki[756]
3 - svick[505]
3 - Casebash[169]
3 - Fatal510[866]
3 - Dan Atkinson[587]
3 - Lucas McCoy[239]
2 - Alexandre Rafalovitch[2883]
2 - tsudot[2662]
2 - Roy Tang[2300]
2 - Greg Bray[150]
2 - Robert Love[1578]
2 - Shane[1270]
2 - Frank Krueger[2203]
2 - David[2118]
2 - pierocampanelli[1384]
2 - Charles Stewart[2083]
2 - voyager[20]
2 - coobird[525]
1 - crucible[1986]
1 - Peter Ajtai[2618]
1 - Nicolas Raoul[2679]
1 - DC01[2603]
1 - Evan[26]
1 - Can Berk Güder[636]
1 - user1711[1711]
1 - Artefacto[1179]
1 - Shimmy[2134]
1 - Michal[1918]
1 - Fernando[1856]
1 - JL01[1911]
1 - Mattias Konradsson[564]
0 - chanchal1987[2951]
0 - zengr[1416]
0 - RichOrr[1749]
0 - Bidhan Baruah[2735]
0 - arinte[2616]
0 - Ngu Soon Hui[216]
0 - hvgotcodes[2532]
0 - Dinis Cruz[1676]
0 - scunliffe[37]
0 - pufferfish[1689]
0 - chalup[892]
0 - Cristian Castiblanco[109]
0 - stacker[1339]
0 - Vitaly Polonetsky[1771]
0 - Vicky[2139]
0 - theplic[2125]
0 - Simon Brown[1177]
0 - C.W.Holeman II[1942]
0 - Heavy Bytes[939]
0 - Jim McKeeth[267]
0 - Wei Hu[1230]
0 - Chris S[1174]
0 - ADB[705]
0 - Matt Huggins[1097]
0 - Alienfluid[1044]
0 - Portman[168]
-1 - peter.newhook[1924]
-2 - ripper234[298]

Not related to the question/answer: these are the requests issued to generate the above results:

#   Result  Protocol    Host    URL Body    Caching Content-Type    Process Comments    Custom  
1   200 HTTP    api.stackapps.com   /1.0/questions?pagesize=100&key=foo-bar-fu  15,046  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:8544            
2   200 HTTP    api.stackapps.com   /1.0/questions?page=2&pagesize=100&key=foo-bar-fu   14,622  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:8544            
3   200 HTTP    api.stackapps.com   /1.0/questions?page=3&pagesize=100&key=foo-bar-fu   15,153  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:8544            
4   200 HTTP    api.stackapps.com   /1.0/questions?page=4&pagesize=100&key=foo-bar-fu   14,174  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:8544            
5   200 HTTP    api.stackapps.com   /1.0/questions?page=5&pagesize=100&key=foo-bar-fu   14,339  private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:8544            
6   200 HTTP    api.stackapps.com   /1.0/questions?page=6&pagesize=100&key=foo-bar-fu   3,460   private     application/json; charset=utf-8 jetbrains.resharper.taskrunner.msil:8544            
            

1

JavaSript / Soapi.JS


using Soapi.JS


   Soapi.RouteFactory("api.stackapps.com", apiKey) // get all users
    .Questions({ pagesize: 100 }).getPagedResponse(function(data) {

        var values = {}, keys = [];
        // add up the score for each user
        for (var i = 0; i < data.items.length; i++) {
            var q = data.items[i];
            var u = q.owner;
            if (u) {
                if (!values[u.user_id]) {
                    keys.push(u.user_id);
                    values[u.user_id] = {
                        display_name: u.display_name,
                        score: 0
                    };
                }
                values[u.user_id].score += q.score;
            }
        }

        // sort and dump
        var output = "";
        for (var i = 0; i < keys.sort(function(a, b) {
            return values[b].score - values[a].score;
        }).length; i++) {
            output += values[keys[i]].score + " " + values[keys[i]].display_name + "\r\n";
        }

        document.getElementById("output").innerHTML = output;

});

Output:

266 George Edison
265 code poet
187 Kevin Montrose
88 systempuntoout
77 Soviut
71 S.Mark
70 Felix
70 Farseeker
65 lfoust
50 Adam Wright
42 Dave Swersky
38 Steffen Opel
38 carson
38 Edan Maor
36 Franci Penov
32 Lucas Jones
31 Bill the Lizard
29 Dave DeLong
28 Thomas McDonald
28 Mark Rushakoff
25 Jedi Master Spooky
25 Shay Erlichmen
24 johnwards
24 swanson
23 TheHurt
22 codeka
22 Catchwa
21 Dennis Williamson
20 radius
20 SztupY
17 jjnguy
17 Matt S.
17 ColinD
17 Kyle Cronin
16 Yacoby
16 Jeff Atwood
16 Ricky
16 Jason
15 Nick Presta
15 Adam
15 eWolf
14 Joel Potter
13 Ryan Brunner
12 RichH
11 Igor Zevaka
11 InfinitiesLoop
11 Koning Baard
11 adrianbanks
10 nabeelmukhtar
10 Martin Plante
9 phsr
8 Nathan Reed
8 Robert Munteanu
8 Peter Mourfield
8 Jan Fabry
7 conmulligan
7 Josh Kelley
6 balpha
6 blork
6 Jonathan
6 Michael B.
6 Kevin
6 Peter Mortensen
6 Martijn Laarman
5 Soldier.moth
5 TheKaptain
5 fukas78
5 chenyuejie
5 mlaw
5 James A. Rosen
5 Tim Post
5 Riduidel
5 Joe
5 mikej
5 Chacha102
4 AidenMontgomery
4 Nathan Voxland
4 bpedro
4 Robert Cartaino
4 jmoy
4 Nippysaurus
4 iconiK
4 denny
4 John Bristowe
4 Mark Hurd
4 Matthew Pelser
3 James Johnson
3 tonklon
3 Matt Culbreth
3 magcius
3 Bialecki
3 svick
3 Casebash
3 Maxim Zaslavsky
3 daltojr
3 Fatal510
3 Dan Atkinson
3 Lucas McCoy
2 Alexandre Rafalovitch
2 tsudot
2 Roy Tang
2 Shane
2 Frank Krueger
2 David
2 pierocampanelli
2 Charles Stewart
2 Robert Love
2 Greg Bray
2 voyager
2 coobird
1 crucible
1 Peter Ajtai
1 Nicolas Raoul
1 DC01
1 Evan
1 Can Berk Güder
1 Shimmy
1 Michal
1 Fernando
1 JL01
1 user1711
1 Artefacto
1 Mattias Konradsson
0 chanchal1987
0 zengr
0 RichOrr
0 Bidhan Baruah
0 arinte
0 Ngu Soon Hui
0 hvgotcodes
0 Dinis Cruz
0 Vitaly Polonetsky
0 Vicky
0 theplic
0 Simon Brown
0 C.W.Holeman II
0 pufferfish
0 chalup
0 Cristian Castiblanco
0 stacker
0 Heavy Bytes
0 Jim McKeeth
0 Wei Hu
0 Chris S
0 ADB
0 Matt Huggins
0 Alienfluid
0 Portman
0 scunliffe
-1 peter.newhook
-2 ripper234

Requests generated (elapsed time 3 seconds)


#   Result  Protocol    Host    URL Body    Caching Content-Type    Process Comments    Custom  
1   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=1&jsonp=Soapi._internal._callback0  14,486  private     application/json; charset=utf-8 iexplore:3360           
2   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=2&jsonp=Soapi._internal._callback1  14,704  private     application/json; charset=utf-8 iexplore:3360           
3   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=3&jsonp=Soapi._internal._callback2  15,407  private     application/json; charset=utf-8 iexplore:3360           
4   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=4&jsonp=Soapi._internal._callback3  14,012  private     application/json; charset=utf-8 iexplore:3360           
5   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=5&jsonp=Soapi._internal._callback4  14,457  private     application/json; charset=utf-8 iexplore:3360           
6   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=6&jsonp=Soapi._internal._callback5  4,234   private     application/json; charset=utf-8 iexplore:3360           
7   200 HTTP    api.stackapps.com   /1.0/questions?key=foo-bar-fu&pagesize=100&page=7&jsonp=Soapi._internal._callback6  204 private     application/json; charset=utf-8 iexplore:3360           


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .