【发布时间】:2011-05-30 19:15:53
【问题描述】:
我需要使用普通的旧 Javascript 或 jQuery 对这种格式的数组进行排序:
var array = [6, 3, 18, 'four', 8, 'five', 6, 'nineteen', 'eight', 'two'];
// Write some javascript or jQuery code below to make sure that
// the integers in the array are correctly ordered from lowest
// to highest.
// Strings should be ignored, remaining in the same order.
// For example, the result for the array above should be:
// [3, 'four', 'five', 6, 6, 8, 18, 'nineteen', 'eight', 'two']
console.log(array);
【问题讨论】:
-
为什么“四”和“五”在第二和第三位?
-
为什么最后有一个“二”?
-
您的示例结果不符合要求
-
@CanSpice 和@Ellipsis...:应该忽略字符串“二”、“四”和“五”。如果我使用像“asdf”和“jkjk”这样的字符串,可能会更清楚。字符串的值在问题的范围内并不重要——只是它们的顺序很重要。 @Patricia:我的示例定义了要求。
-
不,我的意思是,如果您忽略非整数,那么为什么要移动“四”和“五”?输出数组不应该是
[3, 6, 6, 'four', 8, 'five', 18, 'nineteen', 'eight', 'two']吗?
标签: javascript jquery arrays algorithm sorting