Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:06:07+00:00 2026-05-24T09:06:07+00:00

I have a case where I’m provided with one UL, and an unknown number

  • 0

I have a case where I’m provided with one UL, and an unknown number of LIs. I’m looking to use JS to split the LIs into 2 ULs (I’m using jQuery). I’m able to split the ULs evenly by number of LIs, but I’d like to split it based on the height of each LI as well, so that both ULs are close to the same height.

Any help with this would be appreciated, I don’t feel like I’m getting anywhere with the approach I started with.

Thanks.

EDIT: JS code I currently have. The HTML is just a straight UL/LI, each LI can be of varying height.

var $sections = $('div.subsection');

$sections.each(function(){
  var $section = $(this);

  var $list = $section.children('ul');
  var $items = $list.children('li');
  var itemCount = $items.size();
  var leftover = itemCount % 2;
  var itemsPerColumn = Math.floor(itemCount / 2);
  var $newList = $('<ul />');

  $items.each(function(){
    var $this = $(this);
    var index = $items.index($this);

    if (index >= (itemsPerColumn + leftover)) {
      $this.remove().appendTo($newList);
    }
  });

  $list.after($newList);

  _equalizeListHeights();

  function _equalizeListHeights(){
    var listHeight = $list.height();
    var newListHeight = $newList.height();

    if (listHeight > newListHeight){
      var $lastItem = $list.children('li:last');
      var lastItemHeight = $lastItem.height();

      if (listHeight - lastItemHeight > newListHeight + lastItemHeight){
        $lastItem.remove().prependTo($newList);
        _equalizeListHeights();
      }
    }
  }

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:06:09+00:00Added an answer on May 24, 2026 at 9:06 am

    I think I can at least see the approach here:

    1. Calculate the total height of all the list items (total), and store all the individual heights
    2. Calculate the height of one list (total / 2)
    3. Determine an algorithm to sum a set of heights to come as as possible to total / 2, without exceeding it.
    4. Put the elements with these heights into the first list, and put the rest into the second

    Step 3 is the tricky bit. It’s related to the Subset Sum Problem.


    EDIT

    Here’s a brute-force algorithm which solves your problem. It doesn’t run on window.resize, because that would be silly. If you want to see it change, resize the result window, then push run.

    //Sum a jQuery wrapped array
    $.fn.sum = function() {
        var total = 0;
        this.each(function() { total += this; });
        return total;
    };
    //Mask elements with a bitmask
    $.fn.mask = function(mask) {
        return this.filter(function(i) {
            return (mask >> i) & 1;
        })
    }
    
    //Get the sizes, and sneakily return a jQuery object
    var sizes = $('.first li').map(function() { return $(this).outerHeight() });
    
    var total = sizes.sum();
    var maxTotal = total / 2;
    
    var best = {
        total: 0,
        mask: 0
    }
    
    for (var subsetMask = 1; subsetMask < (1 << sizes.length); subsetMask++) {
        //Sum all the heights in this subset
        var subsetTotal = sizes.mask(subsetMask).sum();
    
        //New optimal solution?
        if (subsetTotal > best.total && subsetTotal <= maxTotal) {
            best = {
                total: subsetTotal,
                mask: subsetMask
            };
        }
    }
    
    $('.first li').mask(best.mask).appendTo('.second');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case where a click handler is defined/assigned in one jQuery code
I have a case that keeps coming up where I'm using a ListView or
A system I, lamentably, have to use everyday has all caps case sensitive user
I have one Case form which is resolved,and i want to edit the case
I have a case where using a JOIN or an IN will give me
I have a case where I want to validate an entered vat number. Each
I have a case where a 3rd party ad is bleeding through my modal
I have a case where if a SharePoint site owner decides to break permissions
I have a case where the child view sends notification to its parent view.
I have a case where i'm getting formatted text with <a href ...> ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.