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

Comments (3)

  • Fromm

    Sunday, 18 December, 2016 02:33 PM

    Nice

  • Patricia

    Tuesday, 04 October, 2016 05:23 AM

    Hello my name is Patricia and I just wanted to drop you a quick message here instead of calling you. I discovered your Sort array in ascending and descending order -TheOneblogs.com page and noticed you could have a lot more visitors. I have found that the key to running a popular website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get keyword targeted visitors from and they let you try their service for free for 7 days. I managed to get over 300 targeted visitors to day to my website. http://misdivi.de/at - Unsubscribe here: http://misdivi.de/e8

  • replica orologi serpenti bulgari donna

    Sunday, 31 July, 2016 08:37 PM

    It was lovely having the grounds and gardens of Cotehele House to ourselves in the evenings. The cottage was very well appointed and comfortable. The NT staff and volunteers were all very helpful and friendly. replica orologi serpenti bulgari donna http://www.bzero1jewelry.net/it/

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