0

Possible Duplicate:
HTML5 local storage sort

I am storing data into a localstorage using HTML5 I need to have it sort in the order I put them in. Originally, I had them start with Q: and then appended data to it. I found out taht when I retrieved the data, it did not come out in the same order I entered them in. I then did someting like 1Q:.., 2Q:.. + ..

but found that it again did not sort in the order I needed them in.

      localStorage.getItem(... )

Wondering how I can tell it what sort order to retrieve the data in?

I have the key as such:

1Q:fdf
2Q:dfdf
3Q:dfdf

For the value is is random values. I as hoping that having the key in a certain order may make it work.

3
  • 1
  • edited my answer in response to your edit.
    – Christoph
    Apr 6, 2012 at 19:21
  • FWIW Webkit browsers (Chrome, Opera) seem to arrange the key/value pairs in alphabetical order by key. Although, as with most JS objects, this order should not be trusted. Firefox returns the keys in no discernible order.
    – MrWhite
    Jun 25, 2015 at 18:12

1 Answer 1

1

localStorage is a pure key-value store. it doesn't care for orders or anything other than its key-value pairs, which are always stringified.

if you want to do something fancier you might want to use indexed db which belongs to the html5 standard now (or the web sql database which is implemented in some browsers, but was abandoned some time ago) or pass an stringified object which could contain more info, like the value and a possible sort-order.

how exactly did you pass the data to localstorage?

EDIT to your answer to my question:

you could create an array, containing your sorted values and store it's stringified representation. You might need to write your own sort-function for that.

localStorage.setItem(JSON.stringify( <your sorted array> ));

then, to retrieve the value, you can do:

var array = JSON.parse( localStorage.getItem( <your sorted array> ) );
1
  • @Shristoph, how do I add a sorted array along with th data?
    – Nate Pet
    Apr 6, 2012 at 19:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.