How can I order a jQuery wrapped set?
<p>I need to be able to get the top-most ".ui-dialog" element in a wrapped set.</p>

<p>My first thought was to do something like this:</p>

<pre><code>var topDialog = $(".ui-dialog").orderBy("[style*=z-index]").eq(0);
</code></pre>

<p>Is there a nicer way to do this than writing a loop to check the values?</p>

<p><strong>Edit:</strong> Just to clarify... I need to be able to get the top-most element at any given point during the life of the page (after multiple dialogs are opened and closed). Nick Craver's answer to use the maxZ variable doesn't seem to work since the variable is not decremented when dialogs are removed.</p>

<p>Here's the loop I'm using now when I close a dialog that seems kind of ugly to me:</p>

<pre><code>// Enable printing of the top-most dialog or #page
if ($(".ui-dialog").length &gt; 0) {

    var top = $(".ui-dialog").first();

    $(".ui-dialog").each(function () {
        if ($(this).css("z-index") &gt; top.css("z-index")) {
            top = $(this);
        }
    });

    top.removeClass("dont-print");

} else {

    $("#page").removeClass("dont-print");

}
</code></pre>
