2013-07-21 62 views
1

我正在一個網站上的代碼位,我試圖通過成員id(我已經在下面的代碼中留下評論這是什麼)排序結果,它似乎沒有排序的順序結果雖然如此,我想我一定是做錯了什麼。有誰知道這個問題是什麼,也許我可以如何將結果數量限制在10左右?Javascript排序方法

var httpRequestObject = $.ajax({ 
type: "POST", 
contentType: "application/json; charset=utf-8", 
url: "/Wcf/Search.svc/TestSearch", 
dataType: "json", 
success: function (response) { 

    if (response != null && response.d != null) { 
     var data = response.d; 
     if (data.ServiceOperationOutcome == 10) { 

      var profileList = data.MemberList; 
      if (profileList != null && profileList.length > 0) { 
       for (var i = 0; i < profileList.length; i++) { 
        var profile = profileList[i]; 

        // sort var 
        var memberId = (profile.MemberId); 


        if (profile != null) { 
         var clonedTemplate = $('.profile-slider #profile').clone(); 
         $(clonedTemplate).removeAttr('style').removeAttr('id'); 
         $(clonedTemplate).find('img').attr("src", profile.ThumbnailUrl).attr("alt", profile.Nickname).wrap('<a></a>'); 
         $(clonedTemplate).appendTo('.profile-slider'); 

         // sort 
         $(memberId).sort(); 
        } 
       } 

      } 
     } 

     else { 
      alert("Error code " + String(data.ServiceOperationOutcome)); 
     } 
    } 

    else { 
     alert("Null data"); 
    } 
}, 

error: function (jqXHR, textStatus, errorThrown) { 
    alert(errorThrown); 
} 

});

+0

你打算用'$(memberId).sort();'?這裏有什麼'memberId'? –

回答

3

正如adeneo說的那樣,您可能想要對成員列表進行排序。

profileList = profileList 
    .filter(function (arg) {return arg !== null;}) // remove nulls 
    .sort(function(a, b) { 
    return a.MemberId < b.MemberId ? -1 : 1; // the < operator works for numbers or strings 
    }); 
+0

Brillaint!感謝兩者 – Adam

3

jQuery的沒有一個排序方法,但有Array.sort()這可能是你在找什麼,但輪廓似乎是陣列data.MemberList內的對象,所以你應該遍歷它那種之前:

var profileList = data.MemberList; // array 
profileList.sort(function(a,b) { 
    return a.MemberId.localeCompare(b.MemberId); 
}); 

for (var i = 0; i < profileList.length; i++) { 
    // do stuff to each item in the now sorted array 
}