Page 1 of 1

ascending order ??

PostPosted: Fri May 01, 2015 6:29 am
by martinp
I have an onscreen scoreboard for quiz nights:
The teams appear as:

[Team1] : [ScoreTeam1]
[Team2] : [ScoreTeam2]
[Team3] : [ScoreTeam3]
...etc (upto 20)

At the moment when i enter scores, they are in random order and show on the score board as they are...
eg:
THE BRAINIACS : 22
NEW INN : 14
FUNKY MAN : 26
etc..

Can anyone suggest how i can make it so that the TOP SCORE is always at the top and other are arranged in order of score?

eg:
FUNKY MAN : 26
THE BRAINIACS : 22
NEW INN : 14
etc..

Thanks guys..

Re: ascending order ??

PostPosted: Fri May 01, 2015 7:31 am
by Gaev
martinp:

The simplest way would be to store/sort records in a Database ... either using NeoDB Pro ... or (the free) NeoDB.

Each record would have 2 fields (columns) ... TeamName and TeamScore

Use dbpSort or dbfSort to sort on the TeamScore field ... then read all the records from top (dbpFirst/dbfFirst) down (dbpNext/dbfNext) ... with each iteration of the Loop (until all records are read), you copy the field contents of the current record into your onscreen variables.

If you are familiar with Javascript, you could pass the data to a Javascript Variable, have it sort the data ... and return it to NeoBook variables.

Re: ascending order ??

PostPosted: Fri May 01, 2015 7:34 am
by martinp
thanks Gaev,
too complicated for my very limited experience.....but thank you for the suggestion

Re: ascending order ??

PostPosted: Fri May 01, 2015 3:20 pm
by martinp
still struggling......can anyone please help!

Thanks

Re: ascending order ??

PostPosted: Sat May 02, 2015 7:00 am
by Gaev
martinp:

Step 1:

Copy this code and paste it into notepad.exe ...
Code: Select all
{NeoBook Function}
Version=5.80
Language=JScript
{End}
//var teams = ["THE BRAINIACS:22", "NEW INN:14", "FUNKY MAN:26"];
var teamsUnsorted = nbGetVar( '[teamsUnsorted]' );

var teams = teamsUnsorted.split(",");

teams.sort(function(a, b){

   var valuesA = a.split(":");
   var ScoreA = parseInt(valuesA[1]);
   var valuesB = b.split(":");
   var ScoreB = parseInt(valuesB[1]);
   //alert(ScoreA + " ... " + ScoreB);
   return(ScoreA-ScoreB);

});

nbSetVar( '[teamsSorted]', teams );


Step 2:

Save it in the Functions folder within your NeoBook folder ... name the file martinSort ... no file extension

Step 3:

In your pub, use this code ...
Code: Select all
... normally, you would already have these variables populated by other parts of your pub
... so remove these lines after your initial test to verify that these three teams do sort as expected
SetVar "[Team1]" "THE BRAINIACS"
SetVar "[ScoreTeam1]" "22"
SetVar "[Team2]" "NEW INN"
SetVar "[ScoreTeam2]" "14"
SetVar "[Team3]" "FUNKY MAN"
SetVar "[ScoreTeam3]" "26"

... combine into a single text string
SetVar "[teamsUnsorted]" ""
Loop "1" "20" "[thisTeam]"
  If "[Team[thisTeam]]" "<>" ""
     SetVar "[teamsUnsorted]" "![teamsUnsorted],[Team[thisTeam]]:[ScoreTeam[thisTeam]]"
  EndIf
EndLoop
... remove leading comma
SubStr "[teamsUnsorted]" "2" "99999" "[teamsUnsorted]"

... now call function to sort it
Call "martinSort"
...AlertBox "teamsSorted" "[teamsSorted]"

... now split the returned text (in variable [teamsSorted]) into its components
StrParse "[teamsSorted]" "!," "[teamInfo]" "[teamCount]"
Loop "1" "[teamCount]" "[thisTeam]"
   ... now split into name and score
   StrParse "[teamInfo[thisTeam]]" "!:" "[thisInfo]" "[junk]"
   SetVar "[Team[thisTeam]]" "[thisInfo1]"
   SetVar "[ScoreTeam[thisTeam]]" "[thisInfo2]"
   ... verify
   AlertBox "Team [thisTeam] of [teamCount]" "[teamInfo[thisTeam]] ... [Team[thisTeam]] and [ScoreTeam[thisTeam]]"
EndLoop
... now [Team1], [Team2] etc. will be in order of [ScoreTeam1], [ScoreTeam2] etc.

Re: ascending order ??

PostPosted: Sat May 02, 2015 9:02 am
by martinp
Thank you, thank you, thank you Gaev!

Placed the step 3 code into the page properties/action.....is that correct??
Seems to work fine except lists low score to high score instead of vice-versa.
Do i have to click the alertbox each time to resort?

Re: ascending order ??

PostPosted: Sat May 02, 2015 9:45 am
by martinp
Gaev,
sorted out the low to high...to high to low....thanks again..

Is there a way to display results without having to press the Alert box OK each time for re-sorting???

Re: ascending order ??

PostPosted: Sat May 02, 2015 11:49 am
by Gaev
martinp:

Placed the step 3 code into the page properties/action.....is that correct??
Seems to work fine except lists low score to high score instead of vice-versa.

sorted out the low to high...to high to low....thanks again..

Two ways ...

1) change this line in the javascript function file ...
Code: Select all
return(ScoreA-ScoreB);
... to ...
Code: Select all
return(ScoreB-ScoreA);


2) change this line in your pub ...
Code: Select all
Loop "1" "[teamCount]" "[thisTeam]"
... to ...
Code: Select all
Loop "[teamCount]" "1" "[thisTeam]"


Is there a way to display results without having to press the Alert box OK each time for re-sorting???

Do i have to click the alertbox each time to resort?

You can remvove this command ...
Code: Select all
AlertBox "Team [thisTeam] of [teamCount]" "[teamInfo[thisTeam]] ... [Team[thisTeam]] and [ScoreTeam[thisTeam]]"
... which is only there to assist in investigating the process (in case things did not work as expected).