Solved

sort direction ..

Posted on 2011-09-06
11
308 Views
Last Modified: 2012-05-12
Hi Experts,
This is continuation of this question
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_27291969.html
Need to change  descending if d order is asc and vice versa


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Zvonko &#42;</title>
    <script>
    function srtodr(sFunc)
        {
            var testString = "{ 'a':[ { 'Name':'123abc','Age':'30','Date':'02/08/1988'}, { 'Name':'125','Age':'25','Date':'30/10/1975'}, { 'Name':'abcfgj','Age':'41','Date':'24/12/1982'},{ 'Name':' fsfdsf','Age':'30','Date':'02/08/1988'},{ 'Name':'','Age':'30','Date':'02/08/1988'},{ 'Name':'AbcTest','Age':'30','Date':'02/08/1988'} ]} ";
            var oJSON = eval("(" + testString + ")");
			conc.innerHTML = "";
			oJSON.a.sort(sFunc);
            for (var i = 0; i < oJSON.a.length; i++)
            {
                conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name;
                conc.innerHTML = conc.innerHTML + " Age = " + oJSON.a[i].Age;
                conc.innerHTML = conc.innerHTML + " Date = " + oJSON.a[i].Date;
                conc.innerHTML = conc.innerHTML + "<br/>";
            }             
        }
	function sortName(a,b){
	  if(a.Name>b.Name) return 1;
	  if(a.Name<b.Name) return -1;
	  return 0;
	}
	function sortAge(a,b){
	  return (a.Age*1)-(b.Age*1)
	}
	function sortDate(a,b){
	  var aDate = a.Date.split('/');
	  var bDate = b.Date.split('/');
	  aDate = new Date(aDate[2],aDate[1],aDate[0]);
	  bDate = new Date(bDate[2],bDate[1],bDate[0]);
	  return aDate.getTime()-bDate.getTime();
	}
	
    
    </script>
</head>
<body onload="srtodr(sortName)">
    <form id="form1" runat="server">
    <div id="conc"></div>
    <div>
      <a href="javascript:void(0)" onclick="srtodr(sortName)"> Sort Name </a> ||
       <a href="javascript:void(0)" onclick="srtodr(sortAge)">Sort Age </a> ||
       <a href="javascript:void(0)" onclick="srtodr(sortDate)">Sort Date </a> 
    </div>
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:Rajar Ahmed
  • 6
  • 5
11 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Test page : http://jsfiddle.net/TH5vA/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Zvonko &#42;</title>
    <script>

var _n = false;
var _a = false;
var _d = false;

    function srtodr(sFunc)
        {
            var testString = "{ 'a':[ { 'Name':'123abc','Age':'30','Date':'02/08/1988'}, { 'Name':'125','Age':'25','Date':'30/10/1975'}, { 'Name':'abcfgj','Age':'41','Date':'24/12/1982'},{ 'Name':' fsfdsf','Age':'30','Date':'02/08/1988'},{ 'Name':'','Age':'30','Date':'02/08/1988'},{ 'Name':'AbcTest','Age':'30','Date':'02/08/1988'} ]} ";
            var oJSON = eval("(" + testString + ")");
			conc.innerHTML = "";
			oJSON.a.sort(sFunc);
            for (var i = 0; i < oJSON.a.length; i++)
            {
                conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name;
                conc.innerHTML = conc.innerHTML + " Age = " + oJSON.a[i].Age;
                conc.innerHTML = conc.innerHTML + " Date = " + oJSON.a[i].Date;
                conc.innerHTML = conc.innerHTML + "<br/>";
            }             
        }
	function sortName(a,b){
	  if(a.Name>b.Name) return 1;
	  if(a.Name<b.Name) return -1;
	  return 0;
	}
	function sortAge(a,b){
	  return (a.Age*1)-(b.Age*1)
	}
	function sortDate(a,b){
	  var aDate = a.Date.split('/');
	  var bDate = b.Date.split('/');
	  aDate = new Date(aDate[2],aDate[1],aDate[0]);
	  bDate = new Date(bDate[2],bDate[1],bDate[0]);
	  return aDate.getTime()-bDate.getTime();
	}
	
    
    </script>
</head>
<body onload="srtodr(sortName)">
    <form id="form1" runat="server">
    <div id="conc"></div>
    <div>
       <a href="javascript:void(0)" onclick="_n?srtodr(sortName):srtodr(sortName2);_n=!_n;"> Sort Name </a> ||
            <a href="javascript:void(0)" onclick="_a?srtodr(sortAge):srtodr(sortAge2);_a=!_a;">Sort Age </a> ||
                <a href="javascript:void(0)" onclick="_d?srtodr(sortDate):srtodr(sortDate2);_d=!d;">Sort Date </a> 
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
Comment Utility
http://jsfiddle.net/TH5vA/8/

unable to sort date when some values  null  in date column
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
and for you a null value is greater of lower than a valid date?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
else you just need to test :

if( a == null ) a = new Date(1970,0,1); // or a = new Date(2200,0,1);
if( b == null ) b = new Date(1970,0,1);

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
Comment Utility
Like numeric if its null its priority of ascending is set to first
similarly i want to sort data ascending if its set to null .

Example
                                          RESULTS
date  records      !           Asc             Desc
null                      !           null          02/08/1990
02/08/1988         !   02/08/1982     02/08/1988
02/08/1982         !     02/08/1988   02/08/1982
02/08/1990         !     02/08/1990             null

Tried this way ..
http://jsfiddle.net/TH5vA/20/
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
ID:36489917 did not help you?
0
 
LVL 18

Author Comment

by:Rajar Ahmed
Comment Utility
yes , i tried dat in the jsfiddle . it was not working...
http://jsfiddle.net/TH5vA/20/
function srtodr(sFunc) {
    var testString = "{ 'a':[{ 'Name':'abcfgj','Age':'41','Date':''},{ 'Name':' fsfdsf','Age':'30','Date':'02/08/1990'},{ 'Name':'','Age':'30','Date':'02/08/1988'},{ 'Name':'AbcTest','Age':'30','Date':'02/08/1982'} ]} ";
    var oJSON = eval("(" + testString + ")");
    conc.innerHTML = "";
    oJSON.a.sort(sFunc);
    for (var i = 0; i < oJSON.a.length; i++) {
        conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name;
        conc.innerHTML = conc.innerHTML + " Age = " + oJSON.a[i].Age;
        conc.innerHTML = conc.innerHTML + " Date = " + oJSON.a[i].Date;
        conc.innerHTML = conc.innerHTML + "<br/>";
    }
}

function sortName(a, b) {
    if (a.Name > b.Name) return 1;
    if (a.Name < b.Name) return -1;
    return 0;
}

function sortAge(a, b) {
    return (a.Age * 1) - (b.Age * 1)
}

function sortDate(a, b) {
   if(( a == null ) || ( a == '' )) a = new Date(1970,0,1); // or a = new Date(2200,0,1);
    if(( b == null )||( b == '' )) b = new Date(1970,0,1);
    var aDate = a.Date.split('/');
    var bDate = b.Date.split('/');
    aDate = new Date(aDate[2], aDate[1], aDate[0]);
    bDate = new Date(bDate[2], bDate[1], bDate[0]);
    return aDate.getTime() - bDate.getTime();
}

function sortName2(a, b) {
    if (a.Name < b.Name) return 1;
    if (a.Name > b.Name) return -1;
    return 0;
}

function sortAge2(a, b) {
    return (b.Age * 1) - (a.Age * 1)
}

function sortDate2(a, b) {
    if(( a == null ) || ( a == '' )) a = new Date(1970,0,1); // or a = new Date(2200,0,1);
    if(( b == null )||( b == '' )) b = new Date(1970,0,1);
    var aDate = a.Date.split('/');
    var bDate = b.Date.split('/');
    aDate = new Date(aDate[2], aDate[1], aDate[0]);
    bDate = new Date(bDate[2], bDate[1], bDate[0]);
    return bDate.getTime() - aDate.getTime();
}
var _n = false;
var _a = false;
var _d = false;

window.onload = function() {
    srtodr(sortName)
}

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
Comment Utility
Check this one : http://jsfiddle.net/TH5vA/31/

function srtodr(sFunc) {
    var testString = "{ 'a':[{ 'Name':'abcfgj','Age':'41','Date':''},{ 'Name':' fsfdsf','Age':'30','Date':'02/08/1990'},{ 'Name':'','Age':'30','Date':'02/08/1988'},{ 'Name':'AbcTest','Age':'30','Date':'02/08/1982'} ]} ";
    var oJSON = eval("(" + testString + ")");
    conc.innerHTML = "";
    oJSON.a.sort(sFunc);
    for (var i = 0; i < oJSON.a.length; i++) {
        conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name;
        conc.innerHTML = conc.innerHTML + " Age = " + oJSON.a[i].Age;
        conc.innerHTML = conc.innerHTML + " Date = " + oJSON.a[i].Date;
        conc.innerHTML = conc.innerHTML + "<br/>";
    }
}

function sortName(a, b) {
    if (a.Name > b.Name) return 1;
    if (a.Name < b.Name) return -1;
    return 0;
}

function sortAge(a, b) {
    return (a.Age * 1) - (b.Age * 1)
}

function sortDate(a, b) {
    if(( a.Date == null ) || ( a.Date == '' )) aDate = ["01","01","1970"]; else aDate = a.Date.split('/');
    if(( b.Date == null )||( b.Date == '' )) bDate = ["01","01","1970"]; else bDate = b.Date.split('/');
    aDate = new Date(aDate[2], aDate[1], aDate[0]);
    bDate = new Date(bDate[2], bDate[1], bDate[0]);
    return aDate.getTime() - bDate.getTime();
}

function sortName2(a, b) {
    if (a.Name < b.Name) return 1;
    if (a.Name > b.Name) return -1;
    return 0;
}

function sortAge2(a, b) {
    return (b.Age * 1) - (a.Age * 1)
}

function sortDate2(a, b) {
    if(( a.Date == null ) || ( a.Date == '' )) aDate = ["01","01","1970"]; else aDate = a.Date.split('/');
    if(( b.Date == null )||( b.Date == '' )) bDate = ["01","01","1970"]; else bDate = b.Date.split('/');
    aDate = new Date(aDate[2], aDate[1], aDate[0]);
    bDate = new Date(bDate[2], bDate[1], bDate[0]);
    return bDate.getTime() - aDate.getTime();
}
var _n = false;
var _a = false;
var _d = false;

window.onload = function() {
    srtodr(sortName)
}

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
Comment Utility
i get error on ie browser but workin in chrome
conc.innerHTML = conc.innerHTML + "Name = " ....
Microsoft JScript runtime error: 'a[...].Name' is null or not an object
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>sortdirection</title>
    <script>
        function srtodr(sFunc) {
            var testString = "{ 'a':[{ 'Name':'abcfgj','Age':'41','Date':''},{ 'Name':' fsfdsf','Age':'30','Date':'02/08/1990'},{ 'Name':'','Age':'30','Date':'02/08/1988'},{ 'Name':'AbcTest','Age':'30','Date':'02/08/1982'},{ 'Name':'abcfgj','Age':'41','Date':''}, ]} ";
            var oJSON = eval("(" + testString + ")");
            conc.innerHTML = "";
            oJSON.a.sort(sFunc);
            for (var i = 0; i < oJSON.a.length; i++) {
                conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name; //error line in ie
                conc.innerHTML = conc.innerHTML + " Age = " + oJSON.a[i].Age;
                conc.innerHTML = conc.innerHTML + " Date = " + oJSON.a[i].Date;
                conc.innerHTML = conc.innerHTML + "<br/>";
            }
        }

        function sortName(a, b) {
            if (a.Name > b.Name) return 1;
            if (a.Name < b.Name) return -1;
            return 0;
        }

        function sortAge(a, b) {
            return (a.Age * 1) - (b.Age * 1)
        }

        function sortDate(a, b) {
            if ((a.Date == null) || (a.Date == '')) aDate = ["01", "01", "1970"]; else aDate = a.Date.split('/');
            if ((b.Date == null) || (b.Date == '')) bDate = ["01", "01", "1970"]; else bDate = b.Date.split('/');
            aDate = new Date(aDate[2], aDate[1], aDate[0]);
            bDate = new Date(bDate[2], bDate[1], bDate[0]);
            return aDate.getTime() - bDate.getTime();
        }

        function sortName2(a, b) {
            if (a.Name < b.Name) return 1;
            if (a.Name > b.Name) return -1;
            return 0;
        }

        function sortAge2(a, b) {
            return (b.Age * 1) - (a.Age * 1)
        }

        function sortDate2(a, b) {
            if ((a.Date == null) || (a.Date == '')) aDate = ["01", "01", "1970"]; else aDate = a.Date.split('/');
            if ((b.Date == null) || (b.Date == '')) bDate = ["01", "01", "1970"]; else bDate = b.Date.split('/');
            aDate = new Date(aDate[2], aDate[1], aDate[0]);
            bDate = new Date(bDate[2], bDate[1], bDate[0]);
            return bDate.getTime() - aDate.getTime();
        }
        var _n = false;
        var _a = false;
        var _d = false;

        window.onload = function () {
            srtodr(sortName)
        }
    </script>
</head>
<body onload="srtodr(sortName)">
    <form id="form1" runat="server">
    <div id="conc"></div>
    <div>
        <a href="javascript:void(0)" onclick="_n?srtodr(sortName):srtodr(sortName2);_n=!_n;"> Sort Name </a> ||
        <a href="javascript:void(0)" onclick="_a?srtodr(sortAge):srtodr(sortAge2);_a=!_a;">Sort Age </a> ||
        <a href="javascript:void(0)" onclick="_d?srtodr(sortDate):srtodr(sortDate2);_d=!_d;">Sort Date </a> 
    </div>
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
Comment Utility
if( oJSON.a[i].Name != null ) conc.innerHTML = conc.innerHTML + "Name = " + oJSON.a[i].Name; //error line in ie

Open in new window

0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
Comment Utility
awesome .. thanks...!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now