Skip to content
Advertisement

How to order localStorage by KEY ASC?

I save data to localStorage.
To be able to order the localStorage i use milliseconds as key.
(But localStorage doesn’t sort or order, so i need to build a array or object that i can sort by key)

JavaScript

Now i’d like to fetch localStorage and display the data ordered by key asc.

I tried:

JavaScript

So it looks like my ordering function does nothing?
How can i loop out my data from localStorage by key ASC?

The order i wan’t is:
….49
….78
….90

Advertisement

Answer

The easiest and smartest way was commented by @Mr.polywhirl

Just add .sort() in the forEach:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement