要对数组 a 排序,
但a的顺序要根据b的元素大小排序。
如上,正确的答案是:
最终:a = [8,6,3];
因为 b[0] = 1 最小排最后,b[1] = 3最大排最前,b[2] = 2 在中间。 

解决方案 »

  1.   

    要对数组 a 排序,
    但a的顺序要根据b的元素大小排序。
    如上,正确的答案是:
    最终:a = [8,6,3];
    因为 b[0] = 1 最小所以a[0]排最后,b[1] = 3所以a[1]最大排最前,b[2] = 2 在中间。 
      

  2.   

    <script> 
    var a = [3,8,6];
     var b = [1,3,2];
     var c=[];
    for(var i=0;i<b.length;i++){
    c[b[i]-1]=a[i];
    }
    c.sort();
    c.reverse();
    alert(c.toString());
    </script> 
      

  3.   

    var a = [3,8,6]; 
     var b = [1,3,2]; 
    var c=[]; //定义第三个变量
    //根据B的值把A的值赋给C
    for(var i=0;i <b.length;i++){ 
    c[b[i]-1]=a[i]; 

    //使用Js中sort()方法的排序
     c.sort(function compare(a,b){return b-a;});//这个就是吧原来反顺序来
      

  4.   

    创建一下关联,直接用sort就可以排..
    <script type="text/ecmascript">
    var a = [3, 8, 6];
    var b = [1, 3, 2];
    var c = [];
    for (var i = 0, l = a.length ; i < l ; i ++)
    c[i] = [b[i], a[i]];
    c.sort(function (a, b) { return b[0] - a[0]; });alert(c.join('\n'));
    </script>
      

  5.   

    <script> 
    var a = [3,8,6]; 
    var b = [1,3,2]; 
    var c = []; 
    for(var i=0;i <b.length;i++){ 
       c[b[i]-1]=a[i]; //把i位b的值作为c的下标,即顺序.此时下标由小到大排列

    alert(c.reverse()); //数组反序,按下标从大到小排列
    </script> 
      

  6.   

    muxrwc 大致符合我要求。其它的,如我把 a 原来的 a=[3,8,6] 改成 a = [3,8,"a"];后就不符合要求了。但 muxrwc 的可以。
      

  7.   

    写个最原始的
    var a = ["3","8",6];
    var b = [1,3,2];
    var nMaxIndex,nTemp;
    for(var i=0,nLen=b.length;i<nLen-1;i++){
        nMaxIndex=i;
        for(var j=i+1;j<nLen;j++){
            if(b[j]>b[nMaxIndex])
                 nMaxIndex=j;   
        }
        nTemp=b[i];
        b[i]=b[nMaxIndex];
        b[nMaxIndex]=nTemp;
        nTemp=a[i];
        a[i]=a[nMaxIndex];
        a[nMaxIndex]=nTemp;
    }
    alert(a);
      

  8.   

    原来sort是可以传参数的第一次看到,长见识了
      

  9.   

    var a = [3,8,6]; 
     var b = [1,3,2]; 
    var c=[]; //定义第三个变量
    //根据B的值把A的值赋给C
    for(var i=0;i  <b.length;i++){ 
    c[b[i]-1]=a[i]; 

    //使用Js中sort()方法的排序
     c.sort(function compare(a,b){return b-a;});//这个就是吧原来反顺序来 =====================
    这个有问题,如:当 b = [1,0,2] 或 b = [1,8,2] 时,你运行就把原来数组长度变了。
    =====================LZ,貌似不想用sort吧.
    ====================不知道有没有正则的解法?
      

  10.   

    <script type="text/javascript">
    //to http://bbs.51js.com/thread-54108-1-1.htmlArray.prototype.mergeSort1 = function(s, e, b, b1, ta)
    {
    //潦草的改了下那个并归,偶算法比较差
    //如果想用别的可以去http://bbs.51js.com/thread-54108-1-1.html,自己找别的算法改。。
    var temp = 0;
    if (s == null) s = 0;
    if (e == null) e = this.length - 1;
    if (b == null) b = new Array(this.length);
    if (b1 == null) b1 = new Array(this.length);

    if (s >= e) return;
    var m = (s + e) >> 1;
    this.mergeSort(s, m, b);
    this.mergeSort(m + 1, e, b);
    for (var i = s, j = s, k = m + 1; i <= e; ++i)
    {
    temp = (k > e || j <= m && this[j] < this[k]) ? j++ : k++;
    b1[i] = ta[temp];
    b[i] = this[temp];
    }
    for (var i = s; i <= e; ++i) {
    ta[e - i] = b1[i];
    this[e - i] = b[i];
    }
    };
    var a = [3, 8, 6];
    var b = [1, 3, 2];
    b.mergeSort1(null, null, null, null, a);
    alert(a);
    </script>
      

  11.   

    muxrwc 上面的好像运行不了。
      

  12.   


    我想问一下
    为什么要写成 c.sort(function (a, b) { return b[0] - a[0]; }); 
    而不是 c.sort(); 呢,muxrwc 的写法有什么好处么?
      

  13.   

    这个算法,没研究的说...
    所以没改成降序
    刚测试3个的那个竟然通过 - -,我还以为可以呢...
    下面的这个用了个reverse效率灰常高..
    1W数据500ms
    还有,昨天的那个帖子,我的回复只不过没封装,不过用hash的效率绝对是很高的.
    测试了下,大概才200ms左右吧...也是1W数据
    <script type="text/javascript">
    //to http://bbs.51js.com/thread-54108-1-1.htmlArray.prototype.mergeSort1 = function(s, e, b, b1, ta)
    {
    //潦草的改了下那个并归,偶算法比较差
    //如果想用别的可以去http://bbs.51js.com/thread-54108-1-1.html,自己找别的算法改。。
        var temp = 0;
        if (s == null) s = 0;
        if (e == null) e = this.length - 1;
        if (b == null) b = new Array(this.length);
        if (b1 == null) b1 = new Array(this.length);
        
        if (s >= e) return;
        var m = (s + e) >> 1;
        this.mergeSort1(s, m, b, b1, ta);
        this.mergeSort1(m + 1, e, b, b1, ta);
        for (var i = s, j = s, k = m + 1; i <= e; ++i)
        {
            temp = (k > e || j <= m && this[j] < this[k]) ? j++ : k++;
            b[i] = this[temp];
    b1[i] = ta[temp];
        }
        for (var i = s; i <= e; ++i) {
            this[i] = b[i], ta[i] = b1[i];
        }
    };var a = [3, 8, 6, 4];
    var b = [1, 3, 2, 0];for (i = 0 ; i < 10 ; i ++) {
        a[i] = Math.ceil(1000 * 9000 * Math.random());
        b[i] = Math.ceil(1000 * 9000 * Math.random());
    }/*
    530ms左右我测试1W数据时
    for (i = 0 ; i < 10000 ; i ++) {
        a[i] = Math.ceil(1000 * 9000 * Math.random());
        b[i] = Math.ceil(1000 * 9000 * Math.random());
    }
    */document.write('a : ', a, '<br \/>b : ', b);var d = new Date;
    b.mergeSort1(null, null, null, null, a);
    a.reverse();
    document.title = new Date() - d + 'ms';
    b.reverse(); //这个不能算时间内上面已经完成了需求document.write('<hr \/>a : ', a, '<br \/>b : ', b);
    </script>
      

  14.   

    to ls,
    他说要降序,所以要合并下两个数组,在排序...
    :D
    原先ttyp有一篇帖子介绍过
    sort方法,你可以去google艘下.
      

  15.   

    不过,我这里测试sort 1W数据时,只不过才700ms多一点.你怎么说挂了?
      

  16.   

    sort
    Sorts the elements of an array. 
    方法源 Array 
    实现版本 Navigator 3.0, LiveWire 1.0
    Navigator 4.0: modified behavior. 语法
    sort(compareFunction) 
    参数
    compareFunction Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element. 描述
    If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. For example, "80" comes before "9" in lexicographic order, but in a numeric sort 9 comes before 80.
    If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:If compareFunction(a, b) is less than 0, sort b to a lower index than a. If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. If compareFunction(a, b) is greater than 0, sort b to a higher index than a. 
    So, the compare function has the following form:
    function compare(a, b) {
       if (a is less than b by some ordering criterion)
          return -1
       if (a is greater than b by the ordering criterion)
          return 1
       // a must be equal to b
       return 0
    } To compare numbers instead of strings, the compare function can simply subtract b from a:function compareNumbers(a, b) {
       return a - b
    } JavaScript uses a s表 sort: the index partial order of a and b does not change if a and b are equal. If a's index was less than b's before sorting, it will be after sorting, no matter how a and b move due to sorting.The behavior of the sort method changed between Navigator 3.0 and Navigator 4.0. In Navigator 3.0, on some platforms, the sort method does not work. This method works on all platforms for Navigator 4.0.
    In Navigator 4.0, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array. For example, assume you have this script: <SCRIPT>
    a = new Array();
    a[0] = "Ant";
    a[5] = "Zebra"; function writeArray(x) {
       for (i = 0; i < x.length; i++) {
          document.write(x[i]);
          if (i < x.length-1) document.write(", ");
       }
    } writeArray(a);
    a.sort();
    document.write("<BR><BR>");
    writeArray(a);
    </SCRIPT> In Navigator 3.0, JavaScript prints: ant, null, null, null, null, zebra 
    ant, null, null, null, null, zebra In Navigator 4.0, JavaScript prints: ant, undefined, undefined, undefined, undefined, zebra 
    ant, zebra, undefined, undefined, undefined, undefined 示例
    The following example creates four arrays and displays the original array, then the sorted arrays. The numeric arrays are sorted without, then with, a compare function.
    <SCRIPT>
    stringArray = new Array("Blue","Humpback","Beluga")
    numericStringArray = new Array("80","9","700")
    numberArray = new Array(40,1,5,200)
    mixedNumericArray = new Array("80","9","700",40,1,5,200) function compareNumbers(a, b) {
       return a - b
    } document.write("<B>stringArray:</B> " + stringArray.join() +"<BR>")
    document.write("<B>Sorted:</B> " + stringArray.sort() +"<P>") document.write("<B>numberArray:</B> " + numberArray.join() +"<BR>")
    document.write("<B>Sorted without a compare function:</B> " + numberArray.sort() +"<BR>")
    document.write("<B>Sorted with compareNumbers:</B> " + numberArray.sort(compareNumbers) +"<P>") document.write("<B>numericStringArray:</B> " + numericStringArray.join() +"<BR>")
    document.write("<B>Sorted without a compare function:</B> " + numericStringArray.sort() +"<BR>")
    document.write("<B>Sorted with compareNumbers:</B> " + numericStringArray.sort(compareNumbers) +"<P>") document.write("<B>mixedNumericArray:</B> " + mixedNumericArray.join() +"<BR>")
    document.write("<B>Sorted without a compare function:</B> " + mixedNumericArray.sort() +"<BR>")
    document.write("<B>Sorted with compareNumbers:</B> " + mixedNumericArray.sort(compareNumbers) +"<BR>")
    </SCRIPT> This example produces the following output. As the output shows, when a compare function is used, numbers sort correctly whether they are numbers or numeric strings.stringArray: Blue,Humpback,Beluga
    Sorted: Beluga,Blue,Humpback numberArray: 40,1,5,200
    Sorted without a compare function: 1,200,40,5
    Sorted with compareNumbers: 1,5,40,200 numericStringArray: 80,9,700
    Sorted without a compare function: 700,80,9
    Sorted with compareNumbers: 9,80,700 mixedNumericArray: 80,9,700,40,1,5,200
    Sorted without a compare function: 1,200,40,5,700,80,9
    Sorted with compareNumbers: 1,5,9,40,80,200,700 参看
    Array.join, Array.reverse
      

  17.   

    你是不是直接带着我的join测试的?
    sort排序时间只用了仅仅700多ms而join则用了大量的时间...
    上面的那个并归,只比sort快了30%左右...
      

  18.   


    程序直接写就可以!用startime和endtime比较得的
      

  19.   


    呵呵,哪知道他sort方法应该很快了!
      

  20.   

    muxrwc 你的方法真的很快,完全符合要求。难为大家了。谢谢!
      

  21.   

    楼主,你这个不需要用正则就可以解决的.是否你没有测试8楼的代码?原理很简单,就是用b中"每个下标的值"做为新数组的"下标",这样生成的新数组c就是以"b中的值为顺序",且值为"a中的对应位置的值".此时是以"b中的值"从小到大排列,你需要的是从大到小只需要"反序"即可.其实很简单道理,只要想明白即可,不过可能大多数人都想到另一边去了吧.您可以测试一下。相信这个肯定是能解决该问题的。
      

  22.   

    myvicy 谢谢你的好意思。
    当时我以为你的算法与5楼的一样,所以没有测试了(5楼的我测试有些问题。)。
    我测试过了,感觉这个算法也很好。效率也很快。谢谢了。
      

  23.   

    呵呵 myvicy 的算法确实巧妙 确实巧妙 佩服佩服
    muxrwc 的比较中规中矩没有投机取巧 
    我的方法和muxrwc差不多 不多此一举了
      

  24.   

    晕了..
    我看了
    的需求,以为是b的元素也无规律,并不是a的下标的记录.
    只不过是b的元素的位置和a元素的位置所对应而已.
    所以把需求理解成了同步排序而按照chinmo 和 myvicy 的方法
    则是,b记录了a的下标 + 1
    然后排序b并且和a同步...若是这样的话,还可以
    直接
    <script type="text/javascript">
    //c[l - b[i]]=a[i]; 
    var a = ["c","a","b","e"];  
    var b = [1,3,2,4];  
    var c = [];  
    var l = b.length, i = 0;
    for(;i<l;i++) {
       c[l - b[i]] = a[i]; //把i位b的值作为c的下标,即顺序.此时下标由小到大排列 
    }
    alert(c);
    </script>
      

  25.   

    的需求,以为是b的元素也无规律,并不是a的下标的记录. ========> b 的元素是无规律的!!!
      

  26.   

    呵,差点又忘了。
    所以经确认,只有muxrwc符合要求了,不好意思。
      

  27.   

    muxrwc ,你的那个还是有点小问题,如下:var a = ["001",002,11,1];//
    var b = [1];
    var c = [];
    ……
    这时最终正确答案应该是:c = [1];
    但些时用你的函数运行后,c = [4]; 了
      

  28.   


    从他的描述和所给你的例子来说,就应该是这么一个理解是b的元素的位置和a元素的位置所对应而已,还是从他给的例子
     var a = [3,8,6];
        var b = [1,3,2];
    来看,正好b的记录减1正好是a的记录,故有那一写法