Sorting a list alphabetically with a modulus

后端 未结 4 2064
春和景丽
春和景丽 2021-02-08 20:07

I don\'t have any trouble grabbing a list of elements and sorting them alphabetically, but I\'m having difficulty understanding how to do it with a modulus.

###

4条回答
  •  轮回少年
    2021-02-08 21:05

    Here you go. The code is surprisingly simple once you figure it out. I realize you are using jQuery but I'm not familiar enough with it to use its features. This is simple enough that maybe it's not necessary.

    function pivotArray(arr, columns) {
       var l = arr.length, out = [], ind = 0, i = 0;
       for (; i < l; i += 1) {
          out[ind] = arr[i];
          ind += columns;
          if (ind >= l) {
             ind = ind % columns + 1;
          }
       }
       return out;
    }
    

    And here's the test to prove it works (tested in Firefox 3.6.9, IE 6, Chrome 1.0.154.36):

    
    
    
    
    
    

    One more thing: it might be useful to just move the elements around in-place. I almost had script for it but my script was running backwards (as if floats went from top to bottom first). If I get time I'll work on it and post the code.

    P.S. Anyone want to give me pointers on why I had to add 2 to the width calculation for IE6? Wait... it's the borders of the div isn't it?

提交回复
热议问题