(Squarespace) Sort simple list alphabetically

You can use this code to Page Header Injection to handle this request.

<script>
  document.addEventListener('DOMContentLoaded', function() {
function sortListAlphabetically() {
    const listContainer = document.querySelector('.user-items-list-item-container');
    
    if (!listContainer) {
        console.log('List container not found');
        return;
    }
    
    const listItems = Array.from(listContainer.querySelectorAll('.list-item'));
    
    if (listItems.length === 0) {
        console.log('No items to sort');
        return;
    }
    
    listItems.sort((a, b) => {
        const titleA = a.querySelector('.list-item-content__title');
        const titleB = b.querySelector('.list-item-content__title');
        
        if (!titleA || !titleB) {
            return 0;
        }
        
        const textA = titleA.textContent.trim().toLowerCase();
        const textB = titleB.textContent.trim().toLowerCase();
        
        return textA.localeCompare(textB);
    });
    
    listItems.forEach(item => {
        listContainer.appendChild(item);
    });
    
    console.log('List sorted alphabetically');
}
sortListAlphabetically();
  });
</script>