HTML5本地存储排序 [英] HTML5 local storage sort

查看:70
本文介绍了HTML5本地存储排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本地存储来存储用户条目,并在另一个页面上显示条目。我需要一种方法来根据最近的编辑日期和时间对它们进行排序。有没有办法用HTML5做到这一点。如果没有,那么最简单/最有效的方法是什么?

I am using local storage to store user entries and am displaying the entries on another page. I need a way to sort them based on the most recent date and time of edit. Is there a way to do this with HTML5. If not, what's the easiest/most effective way to do so?

感谢您的意见。

Thanks for the inputs.

推荐答案

如果你的键/值有一个固有的顺序(按字母,数字等),那么在它们中加入时间戳可能是多余的。虽然Storage对象没有排序方法,但可以创建一个新的Array()然后对其进行排序。
$ b

If your keys/values have an inherent order to them (alphabetical, numerical, etc), then putting a timestamp in them may be superfluous. Although the Storage object has no sort method, you can create a new Array() and then sort that.

function SortLocalStorage(){
   if(localStorage.length > 0){
      var localStorageArray = new Array();
      for (i=0;i<localStorage.length;i++){
          localStorageArray[i] = localStorage.key(i)+localStorage.getItem(localStorage.key(i));
      }
   }
   var sortedArray = localStorageArray.sort();
   return sortedArray;
}

这个缺点是数组不是关联的,而是JavaScript Array对象的本质。上面的函数通过将键名嵌入到值中解决了这个问题。这样它仍然在那里,你用来显示排序后的数组的功能可以完成将键和值分开的脚步。

The disadvantage to this is that the array is not associative, but that is by nature of the JavaScript Array object. The above function solves this by embedding the key name into the value. This way its still in there, and the functions you'd use to display the sorted array can do the footwork of separating the keys from the values.

这篇关于HTML5本地存储排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆