0

The normal syntax for using tsort to sort a ul would be:

$('ul>li').tsort();

I have the ul as a variable from code similar to:

var myul=$('#mydiv').find('ul');

How can I use the myul variable later in my code with tsort?

Note that I have simplified the details a lot here, suffice to say that it would not be easy to identify the ul without using the variable when I am ready to tsort, so I am wondering if I can get to it this way round instead.

Ben Holness
  • 2,457
  • 3
  • 28
  • 49
  • Try looking at this [What is the easiest way to order a
      /
      in jQuery?](http://stackoverflow.com/questions/304396/what-is-the-easiest-way-to-order-a-ul-ol-in-jquery)
    – user1477388 Jan 07 '14 at 22:14
  • Just `myul.tsort()` or `myul.children().tsort()` -- not sure what `tsort` is. – Jon Jan 07 '14 at 22:17
  • @user1477388 - Still needs the ul followed by li in the $(), not as a variable like I have it – Ben Holness Jan 07 '14 at 22:17
  • @Jon myul.tsort() would sort the ul nodes not the li nodes within the ul ... but adding .children() does the trick – Ben Holness Jan 07 '14 at 22:18
  • I'm not sure I understand. Can't you simply do `$('#mydiv').find('ul').children('li')`? – user1477388 Jan 07 '14 at 22:19
  • @T.J.Crowder - I didn't realise that myul.children() was equivalent to $('ul>li') and it wasn't given as an example in the tsort docs, but it works. Thanks! – Ben Holness Jan 07 '14 at 22:21
  • @BenHolness: Ah, good! I just posted that as an answer, since I'd found the `tsort` docs (hadn't heard of it before) and it looked like it would be. – T.J. Crowder Jan 07 '14 at 22:22

1 Answers1

1

Assuming you're talking about the tinysort plugin, if you have myul, you have access to its children. The docs say you'd just do this:

myul.children().tsort();

Completely gratuitous example (I wanted to play with tinysort): Live Copy | Live Source

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  <script src="http://tinysort.sjeiti.com/src/jquery.tinysort.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <p>In a second, the list below will sort itself.</p>
  <div id="mydiv">
    <ul>
      <li>one</li>
      <li>two</li>
      <li>three</li>
      <li>four</li>
      <li>five</li>
    </ul>
  </div>
  <script>
    setTimeout(function() {
      var myul = $("#mydiv").find("ul");
      myul.children().tsort();
    }, 1000);
  </script>
</body>
</html>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    This is the correct answer, thanks! In the tsort docs, it doesn't give an example with .children() and I didn't realise that was equivalent. – Ben Holness Jan 07 '14 at 22:24
  • :_) this is a stupid question it's so obvious. I didn't think thats what you wanted. Thanks for downvoting my attempt to help you – user1477388 Jan 07 '14 at 22:24
  • 1
    @user1477388 in my question I specifically said "How can I use the myul variable later in my code with tsort?". Your answer did not use that and I knew as explained in my question how to do it the way you answered. – Ben Holness Jan 07 '14 at 22:26
  • also, @user1477388 it may have been obvious to you, but it wasn't to me. We all learn new things about how stuff works all the time and until you learn something, you don't know it. Asking about something that you don't know does not make it a stupid question. – Ben Holness Jan 07 '14 at 22:35
  • You don't have to explain "there are no stupid questions to me" but I'm just saying, the answer is so obvious it almost goes without saying. I was just mad at myself that I didn't realize how obvious it was! – user1477388 Jan 08 '14 at 14:16