blog post

As you're sorting an array of arrays rather than sorting just a series of names, you'll need to do something slightly different for sorting. Try using something such as this:

Source Code 1

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
  </head>
<body style="text-align:center;">
<h2>Sort array in ascending and descending order </h2>

</body>
<script>
var arrayAsc = function (arr) {
     
      var SortByAsc = arr.sort(function(a, b)
            {
              var nA = a.name.toLowerCase();
              var nB = b.name.toLowerCase();
              var nA = a.name;
              var nB = b.name;
              if(nA < nB)
                return -1;
              else if(nA > nB)
                return 1;
             return 0;
            });
      return SortByAsc;
};


var arrayDesc = function (arr) {
     
      var SortByAsc = arr.sort(function(a, b)
            {
              var nA = a.name.toLowerCase();
              var nB = b.name.toLowerCase();
              var nA = a.name;
              var nB = b.name;
              if(nA < nB)
                return -1;
              else if(nA > nB)
                return 1;
             return 0;
            });
      return SortByAsc.reverse();
};


$(document).ready(function(){

var nameArray=eval('[{"id":1,"name":"Amit"},{"id":2,"name":"Suraj"},{"id":3,"name":"Atul"},{"id":4,"name":"Ankit"},{"id":5,"name":"Anuj"}]');
if(nameArray!="" && nameArray!=undefined)
{

/*Ascending Order*/
var oderAsc=arrayAsc(nameArray);
$("#asc").html("<b>Asc Order: </b>");
 $.each(oderAsc,function(i,elem){
$("#asc").append("<span>"+elem.name+"</span>   ");
 }) ; 
/*Ascending Order*/


/*Descending Order*/
var orderDesc=arrayDesc(nameArray);
$("#desc").html("<b>Desc Order: </b>");
 $.each(orderDesc,function(i,elem){
$("#desc").append("<span>"+elem.name+"</span>   ");
 }) ; 
/*Descending Order*/
}

});
</script>
<pre>var nameArray=eval('[{"id":1,"name":"Amit"},{"id":2,"name":"Suraj"},{"id":3,"name":"Atul"},{"id":4,"name":"Ankit"},{"id":5,"name":"Anuj"}]');</pre>

<div id="asc"></div>
<div id="desc"></div>

Source Code 2

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
  </head>
<body style="text-align:center;">
<h2>Sort array in ascending and descending order </h2>

</body>
<script>
var arrayAsc = function (arr) {
     
      return arr.sort(arr);
};


var arrayDesc = function (arr) {
     return arr.sort(arr).reverse();
};


$(document).ready(function(){

var nameArray=eval('["Suraj","Amit","Atul","Anuj","Ankit"]');
if(nameArray!="" && nameArray!=undefined)
{

/*Ascending Order*/
var oderAsc=arrayAsc(nameArray);
$("#asc").html("<b>Asc Order: </b>");
 $.each(oderAsc,function(i,elem){
$("#asc").append("<span>"+elem+"</span>   ");
 }) ; 
/*Ascending Order*/


/*Descending Order*/
var orderDesc=arrayDesc(nameArray);
$("#desc").html("<b>Desc Order: </b>");
 $.each(orderDesc,function(i,elem){
$("#desc").append("<span>"+elem+"</span>   ");
 }) ; 
/*Descending Order*/
}





var numberArray=eval('["3","6","9","-2","5"]');
if(numberArray!="" && numberArray!=undefined)
{

/*Ascending Order*/
var oderAsc=arrayAsc(numberArray);
$("#ascnumber").html("<b>Asc Number Order: </b>");
 $.each(oderAsc,function(i,elem){
$("#ascnumber").append("<span>"+elem+"</span>   ");
 }) ; 
/*Ascending Order*/


/*Descending Order*/
var orderDesc=arrayDesc(numberArray);
$("#descnumber").html("<b>Desc Number Order: </b>");
 $.each(orderDesc,function(i,elem){
$("#descnumber").append("<span>"+elem+"</span>   ");
 }) ; 
/*Descending Order*/
}



});
</script>
<pre>var nameArray=eval('["Suraj","Amit","Atul","Anuj","Ankit"]');</pre>

<div id="asc"></div>
<div id="desc"></div><br/>


<pre>var numberArray=eval('["3","6","9","-2","5"]');</pre>

<div id="ascnumber"></div>
<div id="descnumber"></div>
var numArray = [140000, 104, 99];
numArray = numArray.sort(function (a, b) {  return a - b;  });

  • Share This Story


Pleas give your valuable comments to improve the contents.

about author

Atul kumar shukla


I'm Atul Kumar Shukla, working as a web developer.
Welcome to my Blog. This is my personal blog where i post my own questions and answers , articles about ASP.Net., SQL Server, HTML, CSS, Jquery and many more..

Visit Profile

Leave a comment

Name
E-mail
Url
Message

Get More Stuff
Subscribing to the TheOneblogs newsletter and get intersting stuff and updates to your email inbox.
Your Email

 

Style Switcher

Change Color

×! We use cookies to improve your user experience. More information