If you want to enhance efficiency, there are definitely ways to do so. The current code serves as an illustration of the solution.
Here is your HTML:
<ul class="main-menu">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
And here is the JS code:
$( window ).resize(function() {
var liWidth = 100;
if($(window).height()/$('.main-menu').children().length < liWidth){
console.log('here');
if($('.main-menu li').first().find('ul').length > 0){
$('.main-menu li').first().find('ul').append('<li>'+$('.main-menu li').last().html()+'</li>');
$('.main-menu li').last().remove();
}
else{
$('.main-menu li').first().append('<ul><li>'+$('.main-menu li').last().html()+'</li></ul>');
$('.main-menu li').last().remove();
}
}
else if($(window).height()/($('.main-menu').children().length + 1) > liWidth){
console.log('there');
if($('.main-menu li').first().find('ul li').length === 1 ){
$('.main-menu').append('<li>' +$('.main-menu li').first().find('ul li').html() + '</li>')
$('.main-menu li').first().find('ul').remove();
}
else if ($('.main-menu li').first().find('ul li').length > 1) {
$('.main-menu').append('<li>' +$('.main-menu li').first().find('ul li').last().html() + '</li>')
$('.main-menu li').first().find('ul li').last().remove();
}
}
});
The end result can be viewed on this Fiddle link.