JS如何将这个vb数组排序由大到小排序var valuse = ['52%','100%','100%','66%'];

PHP中的变量赋值,怎么解释$arr = array_values(array_filter(explode('|', $_class[featherclass])));_百度知道
PHP中的变量赋值,怎么解释$arr = array_values(array_filter(explode('|', $_class[featherclass])));
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
采纳数:201
获赞数:298
从最里面的一层开始分析:1、explode('|',$_class[feathclass]):这个就是将变量$_class[feathclass]的值,通过分割字符“|”进行分割开,比如 a|b|c|d,分割后就是数组 array('a','b','c','d');2、array_filter(数组):这个是过滤掉了数组当中的等值FALSE的数据,比如0或空;3、array_values(数组):这个是返回刚才数组的所有值,并建立数字索引。以上3步连接起来,总的意思就是将变量$_class[feathclass]通过”|“进行分割后,过滤空值,并返回数组中的所有值和建立数字索引。比如上面的例子,返回的结果就是:array(0=&'a',1=&'b,2=&'c',3=&'d')
为你推荐:
其他类似问题
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。& Ruby trunk
Array#values_at does not handle ranges with end index past the end of the array
Status:ClosedPriority:NormalAssignee:Target version:
ruby -v:trunkBackport:
Description
When I use Array#values_at I expect that it would be the same as successive calls to (({Array#[]})).
There is one case where this does not hold:
a = [0,1,2,3,4,5]
a[4..6] # =& [4, 5]
a.values_at(4..6) # =& [4,5,nil]
I think this is an inconsistency in the design of (({Array#values_at})). We can look at a more extreme case:
a[4..100] # =& [4, 5]
a.values_at 4..100 # =& [4, 5, nil]
And now it doesn't make any sense.
I think the best solution would be to make (({Array#values_at})) be equivalent to successive calls to (({Array#[]})). I have patched (({rb_range_beg_len()})) to handle the extra case and opened a pull request on github.
Associated revisions
Updated by
Tracker changed from Bug to Feature
It's definitely not a bug.
Updated by
Description updated ()
Updated by
nobu (Nobuyoshi Nakada) wrote:
It's definitely not a bug.
It's not?
How do you explain:
[1, 2, 3].values_at(2...42) # =& [3]
[1, 2, 3].values_at(2..41)
# =& [3, nil]
I feel that both must return the same result.
Moreover, the only acceptable results are either [3] or [3, nil, nil, ... nil]
Updated by
Tracker changed from Feature to Bug
Moving back to &bug&, as no explanation from Nobu.
Updated by
Assignee set to marcandre (Marc-Andre Lafortune)
Target version changed from 1.9.3 to 2.0.0
ruby -v set to trunk
The patch from Mark Rada never made it through, but I concur that the problem is in rb_range_beg_len.
I'll commit the fix below unless there's objection.
I've already fixed RubySpec:
diff --git a/range.c b/range.c
index 61fb643..aa
--- a/range.c
+++ b/range.c
@@ -746,16 +746,16 @@ rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
if (beg & 0)
goto out_of_
if (end & 0)
if (!excl)
/* include end point */
if (err == 0 || err == 2) {
if (beg & len)
goto out_of_
if (end & len)
if (end & 0)
if (!excl)
/* include end point */
len = end -
if (len & 0)
Updated by
marcandre (Marc-Andre Lafortune) wrote:
The patch from Mark Rada never made it through, but I concur that the problem is in rb_range_beg_len.
Hello Marc,
Sorry for not linking to the pull request here, though I did open a pull request on Github here:
Though I think your solution is much nicer. Perhaps the tests I added are still useful.
Updated by
Status changed from Open to Assigned
Assignee changed from marcandre (Marc-Andre Lafortune) to matz (Yukihiro Matsumoto)
Is this a bug?
Updated by
naruse (Yui NARUSE) wrote:
Is this a bug?
I'm very curious: how could it not be bug?
How would you explain that values_at(2...42) != values_at(2..41)?
I simply want to lighten the load for M he has so many issues assigned to him or waiting on him already.
Updated by
marcandre (Marc-Andre Lafortune) wrote:
I simply want to lighten the load for M he has so many issues assigned to him or waiting on him already.
You must wait (or press) matz if a fix of an issue is clear and no other option of the new behavior.
Updated by
naruse (Yui NARUSE) wrote:
marcandre (Marc-Andre Lafortune) wrote:
I simply want to lighten the load for M he has so many issues assigned to him or waiting on him already.
You must wait (or press) matz if a fix of an issue is clear and no other option of the new behavior.
It has been 2 weeks since the last update. Has this issue been forgotten?
Updated by
Assignee changed from matz (Yukihiro Matsumoto) to nobu (Nobuyoshi Nakada)
The trailing nil must be a bug.
Updated by
Status changed from Assigned to Closed
% Done changed from 0 to 100
This issue was solved with changeset .
Mark, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
array.c: fill with nil
array.c (rb_get_values_at): fill with nil out of range.
Updated by
matz (Yukihiro Matsumoto) wrote:
The trailing nil must be a bug.
Was there a reason to change the behavior of [].values_at(42..100)? Could this not lead to incompatibilities?
Nobu: is there a reason not to fix rb_range_beg_len also as per ?
Updated by
Status changed from Closed to Open
Marking as open, since fix doesn't seem to correspond to what Matz wrote.
marcandre (Marc-Andre Lafortune) wrote:
matz (Yukihiro Matsumoto) wrote:
The trailing nil must be a bug.
Was there a reason to change the behavior of [].values_at(42..100)? Could this not lead to incompatibilities?
Nobu: is there a reason not to fix rb_range_beg_len also as per ?
Updated by
Matz, could you please confirm what return value is correct for [].values_at(1..3)?
1) It could be []. This would be compatible with Ruby 1.8 & 1.9.
2) It could be [nil, nil, nil]. This is current behavior in trunk after Nobu's commit, but with potential incompatibility.
If 1, trunk must be changed
If 2, NEWS must list incompatibility.
Nobu: in either case, any reason not to commit my patch in [#5]?
Updated by
This is a spec change in 2.0 unless any big compatibility problem arise.
Array#values_at should be define as:
def values_at(*idx)
idx.map{|i| self[i]}
So it should be written in the NEWS file.
Updated by
Status changed from Open to Closed
Great, thanks.
NEWS updated.
Also available in:JavaScript Array 教程
作为一个,JS 数组的熟练使用显得非常重要,ECMAScript数组的大小是可以动态调整的,可以随着数据的添加自动增长长度。
创建数值有两种方式:使用Array的构造函数使用数组字面量和对象一样,在使用数组字面量时,也不会调用Array构造函数。
自ECMAScript3后,对于一个网页或者一个全局作用域而言,使用instanceof操作符可以得到满意的结果。
if (value instanceof Array) {}ECMAScript5新增了Array.isArray()方法。
if (Array.isArray(value)) {}转换方法
var colors = ["red", "blue", "green"];alert(colors.toString()); //red,blue,greenalert(colors.valueOf()); //red,blue,greenalert(colors); //red,blue,greenvar person1 = {
toLocaleString : function () {
return "Nikolaos";
toString : function
return "Nicholas";
}};var person2 = {
toLocaleString : function () {
return "Grigorios";
toString : function () {
return "Greg";
}};var people = [person1, person2];alert(people);alert(people.toString());alert(people.toLocaleString());可以使用join()方法来重现toString()方法。
join()方法只接收一个参数,即用作分隔符的字符串,然后返回包含所有数组项的字符串。
如果不给join()传入参数,或者传入undefined,则使用逗号作为分隔符。IE7及更早版本会错误的使用字符串"undefined"作为分隔符。如果数组中某一项的值是null或undefined,那么该值在join()、toLocaleString()、toString()和valueOf()方法返回的结果中以空字符串表示。
ECMAScript为数组提供了push()和pop()方法,以便实现类似栈的行为。
push()方法可以接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度。
pop()方法则从数组末尾移除最后一项,减少数组的Length值,然后返回移除的项。
var colors = new Array();var count = colors.push("red", "green");alert(count); //2count = colors.push("blank");alert(count); //3var item = colors.pop();alert(item); //"blank"alert(colors.length); //2队列方法
shift()能够移除数组中的第一项并返回该项,同时将数组长度减一。结合使用shift()和push()方法,可以像队列一样使用数组。
ECMAScript还为数组提供了unshift()方法,它能在数组的前端添加任意个项并返回新数组的长度。同时使用unshift()和pop()方法,可以从相反的方向来模式队列。IE7以及更早的版本对JavaScript的实现中存在一个偏差,其unshift()方法总是返回undefined而不是数组的新长度。IE8在非兼容模式下会返回正确的长度值。
重排序方法
reverse()和sort()两个方法可以用来重排序。
reverse()会反转数组项的顺序。
var values = [1, 2, 3, 4, 5];values.reverse();alert(values); //5,4,3,2,1在默认情况下,sort()方法按升序排列数组项。为了实现排序,sort()方法会调用每个数组项的toString()方法,然后得到比较字符串,以确定如何排序。
var valuse = [0, 1, 5, 10, 15];values.sort();alert(valuse); //0,1,10,15,5sort()方法可以通过接收一个比较函数作为参数,来指定哪个值位于哪个值的前面。比较函数接收两个参数,如果第一个参数应该位与第二个之前则返回一个负数,如果两个参数相等则返回0,如果第一个参数应该位与第二个之后则返回一个正数。例如:
function compare (value1, value2) {
if (value1 & value2) {
return -1;
} else if (value1, value2) {
}}var values = [0, 1, 5, 10, 15];values.sort(compare);alert(values); //15, 10, 5, 1, 0操作方法
concat()方法会先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。
concat()在没有传递参数的情况下,只是返回当前数组的一个副本。
如果传递给concat()的是一个或者一组数组,该方法会将这些数组中的每一项添加到结果数组中。如果不是数组,这些值就简单地添加到结果数组的末尾。
var colors = ["red", "green", "blue"];var colors2 = colors.concat("yellow", ["black", "brown"]);alert(colors); //red,green,bluealert(colors2); //red,green,blue,yellow,black,brownslice()方法接收一或两个参数,即返回项的起始和结束位置。只有一个参数时,slice()方法返回从该位置到当前数组末尾的所有项。有两个参数时,返回起始和结束之间的项(但是不包括结束位置的项)。slice()不影响原始数组。
var colors = ["red", "green", "blue", "yellow", "purple"];var colors2 = colors.slice(1);var colors3 = colors.slice(1, 4);alert(colors2); //green, blue, yellow, purplealert(colors3); //green, blue, yellow
slice()方法的参数中有一个负数,则用数组的长度加上该数来确定相应的位置。例如:slice(-2, -1)和slice(3, 4)结果是一样的。如果结束位置小于起始位置,则返回空数组。
splice()主要用来向数组中部插入项。删除:需要指定两个参数,删除的起始项和删除的项数。例如:splice(0, 2)会删除数组的前两项。插入:需要指定三个参数,起始位置、0(要删除的项数)和要插入的项。例如:splice(2, 0, "red", "green")会从位置2开始插入字符串"red"和"green"。替换:需要指定三个参数,起始位置、要删除的项数和要插入的任意数量的项。例如:splice(2, 1, "red", "green")。
var colors = ["red", "green", "blue"];var removed = colors.splice(0, 1);alert(colors); //green, bluealert(removed); //red,返回只包含一个项的数组removed = colors.splice(1, 0, "yellow", "orange");alert(colors); //green, yellow, orange, bluealert(removed); //返回一个空数组removed
= colors.splice(1, 1, "red", "purple");alert(colors); //green, red, purple, orange, bluealert(removed); //yellow, 返回只包含一个项的数组位置方法
ECMAScript5添加了两个位置方法:indexOf()和lastIndexOf()。
这两个方法都接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。其中,indexOf()方法从数组的开头(位置0)开始向后查找,lastIndexOf()方法则从数组的末尾开始向前查找。
这两个方法都返回要查找的项在数组中的位置,没找到就返回-1。在比较时,会使用全等操作符(===)。
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];alert(numbers.indexOf(4)); //3alert(numbers.lastIndexOf(4)); //5alert(numbers.indexOf(4, 4)); //5alert(number.lastIndexOf(4, 4)); //3
是否改变原数组的常用方法归纳
js中字符串和数组的常用操作
JavaScript中易混淆的方法之split、splice、slice辨析比较
JS中数组的常用方法(包含es6扩展)
封装数组方法
没有更多推荐了,今天在书上看到两个方法,一个升序方法一个降序方法,记录下来供以后参考!
javascript提供了两个相关方法
颠倒数组顺序;
sort()升序排列数组项
var arr = [1,5,2,10,15];
console.log(arr.sort());
但是sort()方法会根据测试字符串的结果改变顺序,因为数值5虽然小于10,但在进行字符串比较时,”10”则位于”5”的前边,于是数组的顺序就被修改了(如上边代码所示)。
不用说,这种顺序方式在很多情况下都不是最佳方案。因此sort()方法可以接受一个比较函数作为参数,以便我们指定哪个值位于哪个值的前面。
比较函数接受两个参数,如果第一个参数应该位于第二个之前则返回一个负数,如果两个参数相等则返回0,如果第一个参数应该位于第二个之后则返回第一个正数。
代码如下:
var arr = [1,5,2,10,15];
//var arr = ['a','g','f','s','c'];
//var arr = ['hao','an','you','to','happly','hot'];
function compare(value1,value2){
if(value1 & value2){
return -1;
}else if(value1 & value2){
arr.sort(compare);
console.log(arr);
三个不同内容数组返回结果如下:
//[1, 2, 5, 10, 15]
//["a", "c", "f", "g", "s"]
//["an", "hao", "happly", "hot", "to", "you"]
降序排序方法 和上边的代码差不多,通过交换比较函数的返回值即可!
在下边的例子中,比较函数在第一个值应该位于第二个之后的情况下返回1,而在第一个值应该在第二个之前的情况下返回-1。交换返回值的意思时让更大的值排位更靠前,也就是对数组按照降序排序。
代码如下:
var arr = [1,5,2,10,15];
//var arr = ['a','g','f','s','c'];
//var arr = ['hao','an','you','to','happly','hot'];
function compare(value1,value2){
if(value1 & value2){
}else if(value1 & value2){
return -1;
arr.sort(compare);
console.log(arr);
三个数组返回值如下:
//[15, 10, 5, 2, 1]
//["s", "g", "f", "c", "a"]
//["you", "to", "hot", "happly", "hao", "an"]
如果只想反转数组原来的顺序,使用severse()方法要更快一些。
javascript 升序和降序
利用Arrays.sort();方法对相应数组进行升序、降序排列
冒泡排序--对数组中的元素进行降序排列
用Arrays类sort()对数组元素进行升序降序排列
js:数组重排序问题:如何使用sort()方法按数值的大小进行升序或降序排列
没有更多推荐了,昨天搞到三点多都没有搞定数组排序的问题,根源是无法正确获取数组的属性值,今天看了不少数组的资料终于明白了怎样获取数组的属性值了。但后来看prototype源码,发现那里已经有很好的实现了,代码:
keys: function(object) {
var keys = [];
for (var property in object)
keys.push(property);
values: function(object) {
var values = [];
for (var property in object)
values.push(object[property]);
写成标准的方法(数组是object的一种):
function getObjectKeys(object)
var keys = [];
for (var property in object)
keys.push(property);
function getObjectValues(object)
var values = [];
for (var property in object)
values.push(object[property]);
js通过key-value取值
JS循环遍历对象,获取key:value
javascript循环遍历数组输出key value
JS如何简单快捷地接收数组里面的key和value
没有更多推荐了,

我要回帖

更多关于 java数组从小到大排序 的文章

 

随机推荐