Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

How to sort an <ul> using query?

How to sort an <ul> using query?

alphabeticly

2 Answers

Steven Parker
Steven Parker
215,939 Points

For some reason, I couldn't find the API documentation for ".sort()", but apparently jQuery does implement it. It takes a compartor function as an argument just like the version for arrays does:

$("ul").html($("li").sort((a, b) => (a.textContent > b.textContent ? 1 : -1)));

Of course, you'll need more specific selectors if you have more than one list on the page.

any idea how to make load more elemets button for each ul?

Steven Parker
Steven Parker
215,939 Points

Adding elements will be covered in the courses!