1

I have JS object defined as follows -

var item = {};
item[guid()] =
{
    symbol: $('#qteSymb').text(),
    note: $('#newnote').val(),
    date: $.datepicker.formatDate('mm/dd/yy', dt) + " " + dt.getHours() + ":" + minutes,
    pagename: getPageName()
};

At some point in my app I am getting a list of those (Items) back from chrome.storage and I would like to be able to sort it based on the date

Here is what I am doing

var sortable = [];

            $.each(Items, function (key, value) {
                if (value.symbol == $('#qteSymb').text() || all) {                        
                    sortable.push([key, value]);
                }
            });

            console.log(sortable);

            sortable.sort(function (a, b) {
                a = new Date(a[1].date);
                b = new Date(b[1].date);
                return a > b ? -1 : a < b ? 1 : 0;
            });

            console.log(sortable);

It doesn't seem to work. The first and second console.log(sortable); is the same. I have tried changing return a > b ? -1 : a < b ? 1 : 0; to return a < b ? -1 : a > b ? 1 : 0; just to see if I am getting any change to sortable but nothing happens... Thank you~

2
  • 3
    I'd suggest just storing timestamp dt.getTime() as well and simply sort by it like: return a[2] - b[2]; Commented Sep 3, 2013 at 16:43
  • Have you checked that you actually get the desiredDate objects from new Date(a[1].date)? -> use console.log(a.toString()+' and '+b.toString()) in the function. Commented Sep 3, 2013 at 16:48

2 Answers 2

1

Both console.log show the same array because when you use console.log(sortable), sortable is passed by reference, and console output happens AFTER finishing your script - when sortable has been already sorted.

Making your code simple:

var arr = [3,2,1];
console.log(arr); // Produces `[1,2,3]` because its printed
                  // to the console after `arr.sort();`
arr.sort();
console.log(arr); // Produces `[1,2,3]`, as expected

Demo: http://jsfiddle.net/Rfwph/


Workaround

If you want to be able to do console.log with an array to see it before being modifyed, you can use .slice(0) to copy the array, i.e. to obtain another array which contains the same elements as your array.

var arr = [3,2,1];
console.log(arr.slice(0)); // [3,2,1]
arr.sort();
console.log(arr); // [1,2,3]

Demo: http://jsfiddle.net/Rfwph/2/

Edit: better use .slice(0) instead of .slice(), which is supported on FF but ecma262 spec says only end argument is optional.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, that makes sense, thanks Oriol. So that would mean console.log is not a reliable debugging tool? How would you confirm the sort? I put break points in both the console.log(sortable) spots but the array is still the same.
@americanslon If you want to debug arrays, it's better to copy them using .slice()
0

@Oriol:

I just did an identical fiddle http://jsfiddle.net/JaU4g/ , but for me it DID work!

var ar=[3,1,8,4,7,2,4,1]
console.log(ar.join(','));
ar.sort();
console.log(ar.join(','));

giving:

[18:55:31.616] "3,1,8,4,7,2,4,1"
[18:55:31.616] "1,1,2,3,4,4,7,8"

4 Comments

That's because you use ar.join(','), so you convert ar to an string. Then, when you modify ar, the string isn't modified.
That is not right. .join() simply returns a string derived from an array object and leaves the actual array unchanged. Otherwise I would not be able to join it again a second time ...
Yes, I meant that but I didn't explain well. But I wanted to point that you call console.log with an string instead of the object ar. Then, if you change ar, the string won't change.
So, in order for @americanslon to see a difference he should actually generate an independent instance of the array by doing something like var arr1=sortable.slice(0);?

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.