排序月份名称 [英] Sort month names

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

问题描述

使用jQuery按顺序排列月份名称

Sort month names in sequence using jQuery

输入

month = [ "Apr","May","Jun","Oct","Nov","Dec","Jul","Aug","Sep"];

预期产量

month = [ "Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

如何对月份名称进行排序? sort() 仅短字母,字母数字或数字值.

How can sort month name? sort() function only short alphabatic,alphanumeric or numeric value.

推荐答案

以正确的顺序获取一年中所有月份的数组(硬编码以供参考),并使用主月数组的索引作为sort()

Take an array of all months in the year that is in proper order ( hard coded for reference) and use the indexes of master month array as sort criteria in sort()

var allMonths = ['Jan','Feb','Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

var month = [ "Apr","May","Jun","Oct","Nov","Dec","Jul","Aug","Sep"];

month.sort(function(a,b){
    return allMonths.indexOf(a) > allMonths.indexOf(b);
});

// returns  ["Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

演示

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

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