Skip to content
Loading
How to sort array using java script or jQuery?
  • Reshma Bhalekar
    Yes that I understand below is my code
     
     // below is required code snippet
    {
    for(var i=0 ; i
    {
    var accountName = data.d.results[i].LoginName;
    var user = accountName.replace("#","%23");
    $.ajax({
    asynch: false,
    url: "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='"+user +"'",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data1) {
    if(data1.d.UserProfileProperties != undefined)
    {
    var username = data1.d.DisplayName;
    var birthDate = (data1.d.UserProfileProperties.results[61].Value).split(' ')[0] ;
    var birthdayMonth = birthDate.split('/')[0];
    var birthdayDate = birthDate.split('/')[1];
    if( (situation == "Current") || (situation == "prevprev") ||(situation == "nextnext"))
    {
    if(birthdayMonth == CurrentMonth)
    {
    employees.push({name:username , month:monthname[birthdayMonth-1] , bdate:birthdayDate});
    }
    }
    }
    },
    error: function (data1) {}
    });
    }
     
    SortArray(employees);
    }
     
    function SortArray(employees)
    {
    var sortedArray = _.sortBy(employees, 'bdate');
    for(var i=0;i < sortedArray.length ; ++i)
    {
    $("#mytable").append("Name :- " + sortedArray[i].name +" Month :- " + sortedArray[i].month +" Birth Date :- " + sortedArray[i].bdate +"");
    }
    }
     
     
    it gives me sortedArray.length = 0
  • Manav Pandya
    Hello
     
    I told you in Method 1 that if you are using method 1 than you can copy given code in example1 and replace with function in method 2 snippet
     
    Let me know if you need help
     
    Thanks 
  • Reshma Bhalekar
    Hi Manav,
     
    I tried your first method but it is showing "sortedArray" not defined. When I changed it to employees then its showing length as 0 but I use console.log(employees) then it shows all data without sorting.
  • Sagar  Pandurang Kap
    Hi,
    Refer below links :-
     
    https://www.w3schools.com/jsref/jsref_sort.asp
    https://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript
     
    Hope it helps....
  • Manav Pandya
    Hello
     
    For sorting array there are various ways to do , if you want simple step to sort array
     
    Method 1 : 
     
    Than you can use Array.Sort() inbuilt method using javascript 
     
    I have given complete snippet that gives you output in sorted order in ascending order 
     
    Snippet is as follow :
     
    1.   
    2.     function SortArray()   
    3.     {  
    4.         employees.sort();  
    5.         for(var i=0;i < sortedArray.length ; ++i)  
    6.         {  
    7.              $("#mytable").append("Name :- " + sortedArray[i].name  +" Month :- " + sortedArray[i].month  +" Birth Date :- " + sortedArray[i].bdate  +"");  
    8.         }  
    9.     }  
    Just replace above code in my second snippet , you can get desired output
     
    Method 2 :
     
    You can use Underscore.js for that
     
    I have use function : _.sort(Array , 'column_to_be_sorted'); 
     
    Snippet is as follow :
     
    1.   
    2.   
    3.   
    4.     Sorting Array Using Javascript  
    5.     "https://code.jquery.com/jquery-3.3.1.min.js"  
    6.     integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="  
    7.     crossorigin="anonymous">  
    8.     "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js">  
    9.   
    10.   
    11.     

      Sorting Array Using Javascript


        
    12.     "SortArray()">Sort My Table  
    13.     "mytable">  
    14.           
    15.       
    16.       
    17.       
    18.       
    19.   
    20.   
    21.   
     
     
    Output will be :
     
     
     
    And also there are so many ways like custom logic to sort array and many more you can refer from google 
     
    Thanks