我对我正在处理的项目有要求,这对我来说有点棘手。通过分析这些字符串中的日期详细信息对字符串数组进行排序
基本上我有排序基于这些项目的Text
物业项目的数组:是我的项目
这里:
var answers = [],
answer1 = { Id: 1, Text: '3-4 weeks ago' },
answer2 = { Id: 2, Text: '1-2 weeks ago' },
answer3 = { Id: 3, Text: '7-8 weeks ago' },
answer4 = { Id: 4, Text: '5-6 weeks ago' },
answer5 = { Id: 5, Text: '1-2 days ago' },
answer6 = { Id: 6, Text: 'More than 1 month ago' };
answers.push(answer1);
answers.push(answer2);
answers.push(answer3);
answers.push(answer4);
answers.push(answer5);
answers.push(answer6);
我需要分析每一个项目,这样的Text
财产,排序后,阵列看起来像这样:
answers[0] = { Id: 6, Text: 'More than 1 month ago' }
answers[1] = { Id: 3, Text: '7-8 weeks ago' }
answers[2] = { Id: 4, Text: '5-6 weeks ago' }
answers[3] = { Id: 1, Text: '3-4 weeks ago' }
answers[4] = { Id: 2, Text: '1-2 weeks ago' }
answers[5] = { Id: 5, Text: '1-2 days ago' }
逻辑是,最远的日期,更高的优先级它是,所以它应该首先出现在数组中。所以“1-2天”不再是“7-8周”的优先级。
所以逻辑是,我需要提取的数量的值,然后该单元(例如天,周),并以某种方式基于这些细节对数组进行排序。
很老实说,我发现很难想出一个解决方案,我会很感激任何帮助。
编辑: 关于我正在使用的数据,它是从Web服务返回的,我无法更改文本。所以我必须照原样处理。
我最近刚刚在PHP中有类似的问题,我发现PHP函数'sscanf'的组合和'strtotime'很有帮助。由于有一些将PHP函数移植到Javascript的实用程序,因此也可能有一个端口。 - 编辑:发现这里的东西:http://phpjs.org/functions/sscanf/&http://phpjs.org/functions/strtotime/ – Paul
呃,你应该真的存储时间戳,并使用这种日期格式仅用于显示 – Bergi