1

I have two variables in my javascript. A message function : var message A timestamp function : var date(It is of type timestamp)..

Now I have an array which stores messages according to there timestamp. Something like

    var input = []; //dynamic array to store both message and date
    var date = new Date(update.data.history[i].timestamp * 1000); //Getting from Json object
    var date_input = date.toLocaleTimeString();
    var message = update.data.history[i].message;
        for (i to length)
    {
        input.push({ key: date_input, value: message });     //Key refers to date, and value to message
    }
   input.sort(sort_by(key,true,parseInt));

My function sort_by

var sort_by = function(field, reverse, primer){

   var key = function (x) {return primer ? primer(x[field]) : x[field]};

   return function (a,b) {
       var A = key(a), B = key(b);
       return (A < B ? -1 : (A > B ? 1 : 0)) * [1, -1][+!!reverse];                  
   }
}

Now, I tried debugging with firebug and noticed my sorting function is not working. I am using timestamp as key but still no luck. Is there anyway I can sort it according to timestamp and then display it. I tried other sorting solutions on SO, but I guess there is another way to sort when there is datatype like timestamp?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
SandBag_1996
  • 1,570
  • 3
  • 20
  • 50
  • You should post your dynamically populated `input` array here. – The Alpha Aug 03 '12 at 21:36
  • I have added date and message updation. – SandBag_1996 Aug 03 '12 at 21:38
  • 1
    timestamp can simply be an integer and it is easy to sort integers – Ibu Aug 03 '12 at 21:40
  • But apparently it is not sorting. – SandBag_1996 Aug 03 '12 at 21:45
  • At what line does the step debugging fail? 1) Try setting a breakpoint within the sorting function. 2) Then look at every comparison. 3) Often times what happens is that a type is not what you expect, for example you are comparing string values not integers. Comparison of a string version of the timestamp would give unexpected comparison results, for example comparing ASCII values of the letters. Should that be the case, you should be able to detect this while step-debugging. – Nash Worth Aug 03 '12 at 21:53
  • @clintNash : You are right. I just checked. After iterating first time, it shows Nan on A, and B both. Possible solution? – SandBag_1996 Aug 03 '12 at 22:14
  • @UnderDog: Don't only use firebug, but also have a look at the built-in developer console. It will tell you exactly *what* error happened, and *where* it happened. Those informations are crucial to find such an error. Btw: `[1, -1][+!!reverse]`? Wow. Haven't seen that yet, fascinating. Converting to bool and then to a number in order to swap dynamically between two values, great stuff :). – Zeta Aug 03 '12 at 22:24
  • I deserve no credit. Flicked it from here ;) http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects – SandBag_1996 Aug 03 '12 at 22:37
  • Found the error.. Apparently when I parse it, it only recongnizes the first element. Like 6:09:22 PM, it only takes 6. So, no sorting :( – SandBag_1996 Aug 03 '12 at 22:58

2 Answers2

1

In an object literal, you do not have to quote the key:

input.push({ key: date_input, value: message });

However, your function takes three arguments, which aren't given in an object-like notation, so the meaning of key is unknown and will result in a ReferenceError:

input.sort(sort_by(key,true,parseInt));
                   ^
ReferenceError: key is not defined

Use a string as argument instead and it should work:

input.sort(sort_by("key",true,parseInt));
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Apparently it is not working. Wonder why. Even tried conventional method input= input.sort(function (a, b) { return parseFloat(a.key) - parseFloat(b.key) }); Still no luck. It is as if it is not recognizing anything in key – SandBag_1996 Aug 03 '12 at 22:43
  • Found the error.. Apparently when I parse it, it only recongnizes the first element. Like 6:09:22 PM, it only takes 6. So, no sorting :( – SandBag_1996 Aug 03 '12 at 22:56
  • @UnderDog: Try `Number` as `primer` instead of `parseInt`. Also, include a full working example of your problem to your question (provide actual data, no unreferenced variables, etc.). – Zeta Aug 04 '12 at 08:11
  • It was a small problem that I ignored ;) I tried accessing the timestamp function prior converting it into date function, and sorted according to that. It worked :) – SandBag_1996 Aug 06 '12 at 15:01
  • I am sorry I just noticed. Thanks a lot for pointing it out :) – SandBag_1996 Aug 06 '12 at 19:58
  • 1
    @UnderDog - For questions like this, you can add an answer yourself describing how you found the problem, what it was, and what you did about it, then you can accept your own answer. I humbly suggest you do so. Otherwise, someone else with a similar problem will search Google, find this, get excited thinking "Here is the solution!". They read all the comments, they see the guy say "Eureka! I have solved it!" and then.... nothing. No solution. No hint. What happened? I HATE THAT! :D – Chris Baker Aug 08 '12 at 14:24
  • You are absolutely right. But the error I was making was not notable enough to be added as an answer, but nonetheless someone else might also commit the same mistake. I would do it now :) – SandBag_1996 Aug 08 '12 at 14:29
1

The sorting function works perfectly. When I was debugging it with firebug, I saw on the console that even though date-input was "6-29-07 am", it was only taking 6 as the "key", therefore every entry on that particular day was assigned the same key. Therefore, sorting was not able to give the desired output. Therefore, I avoided new date() function in second line, directly took the timestamp as key, sorted accordingly, and then convert it into date-format. Silly error, but took some time.

SandBag_1996
  • 1,570
  • 3
  • 20
  • 50