最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery pass extra variable to sort function - Stack Overflow

programmeradmin3浏览0评论

I need to write a mon sort function. I am using jQuery for sorting. jQuery sort function only accepts two parameters as input. But I want to pass another parameter to that function. How can I do that?

Something like this:

 obj.sort(StringSort);
 obj2.sort(StringSort);

 function StringSort(a, b, desc)
 {
    var aText = $(a).attr(desc).toLowerCase();
    var bText = $(b).attr(desc).toLowerCase();

    if(aText == bText)
      return 0;

    return aText > bText ? 1 : -1;
 }

I need to write a mon sort function. I am using jQuery for sorting. jQuery sort function only accepts two parameters as input. But I want to pass another parameter to that function. How can I do that?

Something like this:

 obj.sort(StringSort);
 obj2.sort(StringSort);

 function StringSort(a, b, desc)
 {
    var aText = $(a).attr(desc).toLowerCase();
    var bText = $(b).attr(desc).toLowerCase();

    if(aText == bText)
      return 0;

    return aText > bText ? 1 : -1;
 }
Share Improve this question edited Nov 25, 2013 at 16:03 BenMorel 36.6k51 gold badges205 silver badges335 bronze badges asked Oct 22, 2012 at 10:27 SathyaSathya 5,3088 gold badges44 silver badges66 bronze badges 2
  • What shall desc contain for values? – tobspr Commented Oct 22, 2012 at 10:29
  • @TobiasSpringer i did some more update to my post. hope you can help me now. – Sathya Commented Oct 22, 2012 at 10:30
Add a ment  | 

3 Answers 3

Reset to default 13

You can create a function that returns a function. The outer function accepts the additional argument and you use the returned function as sorting callback:

function getStringSort(desc) {
    return function(a, b) {
        // this is a closure, you can access desc here
        // this function should contain your parison logic or you just 
        // call StringSort here passing a, b  and desc.
    }
}

obj.sort(getStringSort(someValue));

The inner function has access to all parameters of the outer function since it is a closure [MDN].

I don't believe jQuery has a sort function at all. JavaScript does, on arrays. jQuery and JavaScript are different things (one is a library, the other is a language). Update: Ah, jQuery has an undocumented sort (one of several functions it borrows from Array.prototype), but that fact is not documented and so could change from dot release to dot release. Thanks, Felix.

You can't pass a third argument into the function directly. You can have the function you pass into sort call another function:

// ...assuming `desc` is in scope here here...
obj.sort(function(a, b) {
    return StringSort(a, b, desc);
});

The same desc will get passed to StringSort for each pair of entries being pared. a and b, of course, will be different on each call.

This works because the function we're passing into sort is a closure over the context in which desc is defined. That means the function can access desc (just as functions can access global variables, and for the same reason).

More on closures:

  • Closures are not plicated

Try this:

function sortBy(field){
    return function(a, b){
        if (a.field > b.field)
            return -1;
        if (a.field < b.field)
            return 1;
        return 0;
    };
}
发布评论

评论列表(0)

  1. 暂无评论