Sorting a list alphabetically with a modulus

后端 未结 4 1952
春和景丽
春和景丽 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:00

    See if this will work: http://jsfiddle.net/6xm9m/2

    var newList = new Array();
    var listItem = $('#list > li');
    var mod = 4;
    var colCount = Math.ceil(listItem.length / mod);
    
    listItem.each(function(index) {
        var newIndex = ((index % colCount) * mod) + Math.floor(index / colCount);
        // $(this).text(newIndex);
    
        newList[newIndex] = this;
    });
    
    $('#list').empty();
    
    for(var i = 0; i < newList.length; i++){
        $('#list').append(newList[i]);
    }
    

    Needs improvements, probably, but I'm not really sure how well this works at all.

提交回复
热议问题