Sorting time rankings

0

I am developing a game that performs a function in JavaScript that counts how long the player took to finish until reaching the last phase:

function formatatempo(segs) {
    var min = 0;
    va hr = 0;

    while (segs >= 60) {
        if (segs >= 60) {
            segs = segs - 60;
            min = min + 1;
        }
    }

    while (min >= 60) {
        if (min >= 60) {
            min = min - 60;
            hr = hr + 1;
        }
    }

    if (hr < 10) {
        hr = "0" + hr
    }
    if (min < 10) {
        min = "0" + min
    }
    if (segs < 10) {
        segs = "0" + segs
    }
    fin = hr + ":" + min + ":" + segs
    return fin;
}

In the above case, at the end of execution the variable fin is returned with the time covered. The problem is that I need to store these outputs in a ranking, how could I sort them out from the smallest to the longest?

    
asked by anonymous 15.09.2015 / 01:03

2 answers

0

You just want to catch the amount of seconds the guy spent and display in a readable, correct way? Like, if I spent 65 seconds to figure it out, you want to show me what I spent right? Have you ever thought of doing this:

function foo($seconds) {
  $t = round($seconds);
  return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}

echo foo('290.52262423327'), "\n";
echo foo('9290.52262423327'), "\n";
echo foo(86400+120+6), "\n";

returns

00:04:51
02:34:51
24:02:06
    
15.09.2015 / 20:33
0

At line fin = hr + ":" + min + ":" + segs of this function, you guarantee that the return of this function is a string, of the form HH:MM:SS ; then you can do a direct string comparison to do the sort using the localeCompare () and sort .

Here's an example fiddle: link

    
16.09.2015 / 20:38