Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Appending and Removing Elements

I want to then sort list item in a some order. How does one do that. Is there a function that can help me?

I want to input a number , and sort a list item based on the input. For instances, if I input a one, the list item will go to the top of the list. Append child only just puts it at the end of the list. Is there anything that lets me sort a list numerically based a numeric input?

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

You could try nesting append inside a sort method, so something like

sort(append());

I'm not sure of the precise syntax for that though but I hope it points you in the right direction.

I tried that, and it just tells me that sort is not defined. I think sort only works with arrays not list items. Or am i wrong? I cant seem the find a method that works with list items.

Bob Sutherton
Bob Sutherton
20,160 Points

You can utilize the compare function to sort a list using the sort() method.

var myList = [11, 10, 12, 14, 15];

myList.sort(compare)

function compare(a, b) {
  return a - b;
}

This will give your array a numeric sort.

see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort