【发布时间】:2016-02-17 09:13:02
【问题描述】:
$("#show_times_available_" + pos).find("td span").parent().each(function(){
array.push($(this).attr("id"));
array = array.sort();
});
我的数组获取所有元素并获取它们的 ID 并将其推送到一个数组中,因此我的数组最终将成为;
[ "mon_0600-0", "sun_0700-0", "thu_0600-0", "thu_0700-0", "tue_0300-0", "wed_0700-0" ];
每次将新元素推入数组时,我尝试将这些元素排序为(周一、周二、周三等)。这样我的数组最终会变成;
[ "mon_0600-0", "tue_0300-0", "wed_0700-0", "thu_0600-0", "thu_0700-0", "sun_0700-0" ];
使用基本的sort() 函数会按字母顺序排列,我知道sort() 函数可以接收另一个函数。我只是不确定如何在 vanilla javascript 或 jQuery 中进行设置。是否有 CASE THEN 或使用 when() 和 then() 对这些进行排序的方法?我已经在 google 和 SO 周围搜索过,但什么都没有。
【问题讨论】:
标签: javascript jquery arrays sorting