Javascript关联数组按键值向后反向

时间:2014-11-21 03:13:10

标签: javascript

嗨我在javascript中有一个关联数组

parenthesis=[1: 25, 2: 11, 6: 10, 13: 24, 14: 21, 16: 20];

我希望向后排序,就像

一样
parenthesis=[16:20, 14:21, 13:24, 6:10, 2:11, 1:25];

最有效的方法是什么?

这是完整的代码。我想按照描述^

改变括号的顺序
function parseString(originalString){
originalString = originalString.replace(/ /g, '');
var openParenth = [];
var openPosition = -1;
var closeParenth = [];
var closePosition = -1;
var parenthesis = [];
var i;
var x=0;
var j;
while(originalString.indexOf("(",openPosition+1) != -1){
    openPosition = originalString.indexOf("(",openPosition+1);
    openParenth.push(openPosition);
}
while(originalString.indexOf(")", closePosition+1) != -1){
    closePosition = originalString.indexOf(")",closePosition+1);
    closeParenth.push(closePosition);
}
for(i=0; i<closeParenth.length; i++){
    while(openParenth[x] < closeParenth[i]){
        x++;
    }
    parenthesis[openParenth[x-1]] = closeParenth[i];
    openParenth.splice(x-1, 1);
    x = 0;
}
}

originalString =&#34; 2((2 +4(4 + 5))+((2(4 + 5))/ 3))&#34 ;;

0 个答案:

没有答案