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?

9
  • You should post your dynamically populated input array here. Commented Aug 3, 2012 at 21:36
  • I have added date and message updation. Commented Aug 3, 2012 at 21:38
  • 1
    timestamp can simply be an integer and it is easy to sort integers Commented Aug 3, 2012 at 21:40
  • But apparently it is not sorting. Commented Aug 3, 2012 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. Commented Aug 3, 2012 at 21:53

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

7 Comments

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
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 :(
@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.).
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 :)
@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
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.