Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:45:52+00:00 2026-05-22T11:45:52+00:00

I have a Jquery Script which is depenedent on this Script Sort.js . Can

  • 0

I have a Jquery Script which is depenedent on this Script Sort.js . Can any body How does this works

jQuery.fn.sort = (function(){

    var sort = [].sort;

    return function(comparator, getSortable) {

        getSortable = getSortable || function(){return this;};

        var placements = this.map(function(){

              var sortElement = getSortable.call(this),               
                parentNode = sortElement.parentNode,                      
                // Since the element itself will change position, we have
                // to have some way of storing it's original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );

            return function() {

                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }

                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);

            };

        });

        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });

    };

})();

I got this Script from this link http://qd9.co.uk/projects/jQuery-Plugins/sort/demo.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:45:53+00:00Added an answer on May 22, 2026 at 11:45 am

    It takes essentially three inputs:

    • this is the jQuery object you are calling it on, e.g. if it was $("#my-id").sort() then this would be $("#my-id")
    • comparator is a comparison function of the same type as accepted by JavaScript’s native Array.prototype.sort
    • getSortable is an optional transformation function that is applied to the sorted DOM elements before they are actually sorted. Its behavior is a bit confusing to me still; it appears it would only work if it returned another DOM element?

    It gets the Array.prototype.sort method via the line

    var sort = [].sort;
    

    It then uses it with the comparator function to get a sorted version of the jQuery object, which is “array-like” since it has properties 0, 1, 2, … and a length property:

    sort.call(this, comparator)
    

    It then loops through the result, calling placements[i] on either the DOM element itself (default) or whatever getSortable returns when given a DOM element:

    sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });
    

    So the real magic happens in the creation of the placements array. After some thought, we see that placements[i] is a function that inserts its input into the same place that the ith element of this was, before sorting happened. So calling placement[i] using the ith sorted element as input results in placing the ith sorted element in the ith “slot”, i.e. the place where the ith unsorted element was. The whole business with “flag” nodes inside the placements array creation is so that it can track the position of the original “slots” even after you start replacing them with new, sorted elements.

    Also worth noting: it (rather unnecessarily IMO) wraps itself in an immediately-executing anonymous function which itself returns a function taking the comparator and getSortable parameters. Thus the actual result assigned to jQuery.fn.sort is the two-parameter function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below jquery function $(function(){ $(select#rooms).change(function(){ $.getJSON(/admin/select.php,{id: $(this).val(), ajax: 'true'}, function(j){ var
I have this jquery script which suppose to hide/show div element base on the
I have a jquery script which I need to run only once everything else
I have a JQuery script which acts as a simple image rollover with a
How have you guys handled working with jQuery includes <script type=text/javascript src=jquery.js></script> in Asp.Net
I have this java code: <script src=http://www.google.com/jsapi></script> <script type=text/javascript> google.load(jquery, 1.2.6); $(a#more).click(function() { $(#info_box).show(blind,
Say I have jquery code like this: html += '<div class=index>' + item.index +
I have some jQuery code. I have called an Ajax function file, file.php, that
I have this jQuery code that queries an API on a keyup event (via
I have 2 references to jQuery in my master page which is currently configured

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.