JavaScript - Sorting Arrays Help
Got this script from the Sam's Teach Yourself Javascript In 24 Hours book, and I'm a little confused with regards to this script I came across, or more specifically, the part of the script I've outlined in red.
Code: <script language="javascript" type="text/javascript"> function numcompare(a,b) { return a-b; } nums = new Array (17, 902, 30, 10, 200, 4, 506); sortednums = nums.sort(numcompare); output = sortednums.join(", "); document.write(output); </script> I really don't understand how the numcompare() function works, and the book doesn't explain it very well. Any help with deciphering this piece of code? Thanks in advance. Hashim. Similar TutorialsI need to loop the alphabet and numbers 0-9 to initialize a few thousand arrays. This is for my site and is truly needed. http://www.thefreemenu.com I currently have every array written out and it takes up to much space in my .js file. The majority of my variables are empty but necessary and need to be there (including empty) for my site to work properly. Question is the last part Here's where I'm at. Code: var NewVarLetterOrNum = "a"; eval("_oneofseveralnames_" + NewVarLetterOrNum + "='this part works';"); alert(_oneofseveralnames_a); This creates the variable _oneofseveralnames_a='this part works' Code: var newArrayLetterOrNum = "a"; eval("_oneofseveralnames_" + newArrayLetterOrNum + "= new Array();"); alert(_oneofseveralnames_a) This creates the Array _oneofseveralnames_a=new Array(); and all the values in the array are null, but, now a variable like _nl_a[1]='something' can be used elsewhere because the array exists. This is all that is necessary for now because I can probably set all the variables to be blank with something like Code: i=1 while(i<=20){ _oneofseveralnames_a[i]="1-20"; i++ } alert(_oneofseveralnames_[20]); So now you have what I came to understand in the first few hours. Now to the hard part : ( I can't make multiple array's dynamically. I dont' know if its because I don't understand loops or arrays or what and its very fustrating. As for any answer you might be so kind as to provide, if you could dumb it down that would be greatly appreciated. Code: var newArray =new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z') i=1 while(i<=26){ eval("_nl_" + newArray[i] + "= new Array();"); i++ } alert(newArray[1]) // Is b, but alert(_nl_b) //I can't get _nl_b to exist, I tried everything including taking away the quotes around the letters in every test */ var _nl_a =new Array() var _img_a =new Array() var _h_a =new Array() var _r_a =new Array() var _m_a =new Array() var _yt_a =new Array() var _i_a =new Array() The above arrays are all the array _name_ parts I need but for example, a has 10 parts, a,p2_a,p3_a,.. p10_a. I need 10 pages for each letter of the alphabet and numbers 0-9 and a special all1, p2_all1 ... p10_all1. Overall 2200 arrays that need to be declared. Currently they are all written out. /* Good evening all, I have a form at work that was created to allow for clients information to be inputted into this form. However when the row is inserted it is sorted oldest to newest. I would like to change this however i'm not really sure were to start. Below I have added the javascript code that refers to the addrow function. Any advice would be helpful. Code: function addRow(id){ var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0]; var row = document.createElement("TR") var td1 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td1.appendChild(document.createElement("textarea")) var td2 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td2.appendChild(document.createElement("textarea")) var td3 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td3.appendChild(document.createElement("textarea")) var td4 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td4.appendChild(document.createElement("textarea")) var td5 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td5.appendChild(document.createElement("textarea")) var td6 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td6.appendChild(document.createElement("textarea")) row.appendChild(td1); row.appendChild(td2); row.appendChild(td3); row.appendChild(td4); row.appendChild(td5); row.appendChild(td6); tbody.appendChild(row); var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0]; var row = document.createElement("TR") var td1 = document.createElement("TD") td1.appendChild(document.createTextNode("Remarks:")) var td2 = document.createElement("TD") textArea = document.createElement('TEXTAREA') td2.colSpan="4"; td2.appendChild(document.createElement("textarea")) row.appendChild(td1); row.appendChild(td2); tbody.appendChild(row); } Hi, I would like to sort a table which has inner table using Javascript. When I try to sort, both the outer table and the Inner table are getting sorted. For Eg: I have a table like this: Category1 10.20.30.40 Issue1 sample1 sample2 sample3 Code1 xyz abc The outer table contains Category1,Issue1,Code1 So, after sorting my table should look like this: Category1 10.20.30.40 Code1 xyz abc Issue1 sample1 sample2 sample3 Please provide a solution for this. Hi everyone, I'm new to the forums. I have been coding some simple javascript but it is really new to me. I coded a segment that you can see below but the part in bold doesn't work. I need it to sort by count descending. I'm having major issues with it. Thanks for the help ahead of time Code: <html> <head> <title>ENGO Lab 2.1</title> <script type="text/javascript"> var d=new Date(); var weekday=new Array(7); weekday[0]="Sunday"; weekday[1]="Monday"; weekday[2]="Tuesday"; weekday[3]="Wednesday"; weekday[4]="Thursday"; weekday[5]="Friday"; weekday[6]="Saturday"; var month=new Array(12); month[0]="January"; month[1]="February"; month[2]="March"; month[3]="April"; month[4]="May"; month[5]="June"; month[6]="July"; month[7]="August"; month[8]="September"; month[9]="October"; month[10]="November"; month[11]="December"; document.write("Today is " + weekday[d.getDay()] + "<br />" + d.getFullYear() + "/" + month[d.getMonth()] + "/" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()); </script> <script type="text/javascript"> function sort_descending_count(w){ var sorted = w.sort(); sorted.reverse(); var stuff; var wordCounts = {}; var wordHold = {}; for(i=0; i < w.length; i ++ ){ stuff = sorted[i]; if( wordCounts[stuff] == null ){ wordCounts[stuff] = 1; wordHold[stuff] = stuff; } else{ wordCounts[stuff] += 1; } } var i = 1; wordCounts.sort(); var i = 1; var str = '<table border="1" cellspacing="1" cellpadding="5"><th>Sequence</th><th>Word</th><th>Count</th>'; for( stuff in wordCounts){ str+='<tr><td>'+i+'</td><td>'+stuff+'</td><td>'+wordCounts[stuff]+'</td></tr>'; i = i+1; } str+='</table>'; document.getElementById('change2').innerHTML = txt; } </script> <script type="text/javascript"> function sort_descending(w){ var sorted = w.sort(); sorted.reverse(); var stuff; var wordCounts = {}; for(i=0; i < w.length; i ++ ){ stuff = sorted[i]; if( wordCounts[stuff] == null ){ wordCounts[stuff] = 1; } else{ wordCounts[stuff] += 1; } } var i = 1; var str = '<table border="1" cellspacing="1" cellpadding="5"><th>Sequence</th><th>Word</th><th>Count</th>'; for( stuff in wordCounts){ str+='<tr><td>'+i+'</td><td>'+stuff+'</td><td>'+wordCounts[stuff]+'</td></tr>'; i = i+1; } str+='</table>'; document.getElementById('change2').innerHTML = str; } </script> <script type="text/javascript"> var words; function show_prompt() { var name=prompt("Please input a list of words seperated by a space","Banana Apple Orange"); if (name!=null && name!="") { // split input into array using space var arrayOfWords = name.split(" "); // construct object to save each word and its count var wordCounts = {}; words = arrayOfWords; for(i=0; i < arrayOfWords.length; i ++ ){ var w = arrayOfWords[i]; if( wordCounts[w] == null ){ wordCounts[w] = 1; } else{ wordCounts[w] += 1; } } // output the result var str = '<table border="1" cellspacing="1" cellpadding="5"><th>Word</th><th>Count</th>'; for( w in wordCounts){ str+='<tr><td>'+w+'</td><td>'+wordCounts[w]+'</td></tr>'; } str+='</table>'; document.getElementById('change').innerHTML = str; var button = '<input type="button" onclick="sort_descending(words)" value="Sort with word alphabetically descending">'; var button2 = '<input type="button" onclick="sort_descending_count(words)" value="Sort with word count descending">'; document.getElementById('sorting').innerHTML = button; document.getElementById('sorting2').innerHTML = button2; } else { alert("The Input is Not Valid"); } } window.onload=show_prompt; </script> </head> <body> <br /> <div id="change"> </div> <div id="sorting"> </div> <div id="sorting2"> </div> <div id="change2"> </div> </body> </html>] not sure why this wont sort in asending order... i tend to make little dumb mistakes sorry. :/ Code: <script type="text/javascript"> function sort(nums) { var rangeStart = 0; var rangeEnd = nums.length - 1; var i = 0; var minPosition = rangeStart; while(rangeStart < rangeEnd) { // find minumum for(i = rangeStart; i < rangeEnd; i++) { if(nums[i] <= nums[minPosition+1]) { minPosition = i; } } // swap var temp = nums[rangeStart]; nums[rangeStart] = nums[minPosition]; nums[minPosition] = temp; // change range rangeStart++; } } document.write("<h3>Examples</h3>"); first10 = [2,3,5,7,9,4,8,0,6,1]; document.write("<div>Sorting <tt>["+first10+"]</tt> with current code gives "); sort(first10); document.write("<tt>["+first10+"]</tt></div>"); ages = [19,34,20,66,82,53,88,74,39,13]; document.write("<div>Sorting <tt>["+ages+"]</tt> with current code gives "); sort(ages); document.write("<tt>["+ages+"]</tt></div>"); </script> Not really sure why this isn't working. I've mirrored this webpage from a friend's page - everything is literally identical, save for the references to the databases. His works, and mine doesn't. Here's an example of one of the tables I'm trying to sort: http://www.iblhoops.net/modules.php?...isplay=ratings The code in question for said table: Code: // BEGIN RATINGS TABLE $table_ratings="<table class=\"sortable\"> <thead> <tr bgcolor=$color1><th><font color=$color2>Pos</font></th><th><font color=$color2>Player</font></th><th><font color=$color2>Age</font></th><th><font color=$color2>Sta</font></th><th><font color=$color2>2ga</font></th><th><font color=$color2>2g%</font></th><th><font color=$color2>fta</font></th><th><font color=$color2>ft%</font></th><th><font color=$color2>3ga</font></th><th><font color=$color2>3g%</font></th><th><font color=$color2>orb</font></th><th><font color=$color2>drb</font></th><th><font color=$color2>ast</font></th><th><font color=$color2>stl</font></th><th><font color=$color2>tvr</font></th><th><font color=$color2>blk</font></th><th><font color=$color2>o-o</font></th><th><font color=$color2>d-o</font></th><th><font color=$color2>p-o</font></th><th><font color=$color2>t-o</font></th><th><font color=$color2>o-d</font></th><th><font color=$color2>d-d</font></th><th><font color=$color2>p-d</font></th><th><font color=$color2>t-d</font></th><th><font color=$color2>Foul</font></th><th><font color=$color2>Inj</font></th></tr> </thead> <tbody> "; $i=0; while ($i < $num) { if ($yr == "") { $name=mysql_result($result,$i,"name"); $team=mysql_result($result,$i,"teamname"); $pid=mysql_result($result,$i,"pid"); $pos=mysql_result($result,$i,"altpos"); $p_ord=mysql_result($result,$i,"ordinal"); $age=mysql_result($result,$i,"age"); $inj=mysql_result($result,$i,"injured"); $r_2ga=mysql_result($result,$i,"r_fga"); $r_2gp=mysql_result($result,$i,"r_fgp"); $r_fta=mysql_result($result,$i,"r_fta"); $r_ftp=mysql_result($result,$i,"r_ftp"); $r_3ga=mysql_result($result,$i,"r_tga"); $r_3gp=mysql_result($result,$i,"r_tgp"); $r_orb=mysql_result($result,$i,"r_orb"); $r_drb=mysql_result($result,$i,"r_drb"); $r_ast=mysql_result($result,$i,"r_ast"); $r_stl=mysql_result($result,$i,"r_stl"); $r_blk=mysql_result($result,$i,"r_blk"); $r_tvr=mysql_result($result,$i,"r_to"); $r_sta=mysql_result($result,$i,"sta"); $r_foul=mysql_result($result,$i,"r_foul"); $r_totoff=mysql_result($result,$i,"oo")+mysql_result($result,$i,"do")+mysql_result($result,$i,"po")+mysql_result($result,$i,"to"); $r_totdef=mysql_result($result,$i,"od")+mysql_result($result,$i,"dd")+mysql_result($result,$i,"pd")+mysql_result($result,$i,"td"); $r_oo=mysql_result($result,$i,"oo"); $r_do=mysql_result($result,$i,"do"); $r_po=mysql_result($result,$i,"po"); $r_to=mysql_result($result,$i,"to"); $r_od=mysql_result($result,$i,"od"); $r_dd=mysql_result($result,$i,"dd"); $r_pd=mysql_result($result,$i,"pd"); $r_td=mysql_result($result,$i,"td"); $r_bird=mysql_result($result,$i,"bird"); $draftyear=mysql_result($result,$i,"draftyear"); $exp=mysql_result($result,$i,"exp"); $cy=mysql_result($result,$i,"cy"); $cyt=mysql_result($result,$i,"cyt"); $yearoffreeagency=$draftyear+$exp+$cyt-$cy; } else { $name=mysql_result($result,$i,"name"); $team=mysql_result($result,$i,"team"); $pid=mysql_result($result,$i,"pid"); $r_2ga=mysql_result($result,$i,"r_2ga"); $r_2gp=mysql_result($result,$i,"r_2gp"); $r_fta=mysql_result($result,$i,"r_fta"); $r_ftp=mysql_result($result,$i,"r_ftp"); $r_3ga=mysql_result($result,$i,"r_3ga"); $r_3gp=mysql_result($result,$i,"r_3gp"); $r_orb=mysql_result($result,$i,"r_orb"); $r_drb=mysql_result($result,$i,"r_drb"); $r_ast=mysql_result($result,$i,"r_ast"); $r_stl=mysql_result($result,$i,"r_stl"); $r_blk=mysql_result($result,$i,"r_blk"); $r_tvr=mysql_result($result,$i,"r_tvr"); $r_totoff=mysql_result($result,$i,"r_oo")+mysql_result($result,$i,"r_do")+mysql_result($result,$i,"r_po")+mysql_result($result,$i,"r_to"); $r_totdef=mysql_result($result,$i,"r_od")+mysql_result($result,$i,"r_dd")+mysql_result($result,$i,"r_pd")+mysql_result($result,$i,"r_td"); $r_oo=mysql_result($result,$i,"r_oo"); $r_do=mysql_result($result,$i,"r_do"); $r_po=mysql_result($result,$i,"r_po"); $r_to=mysql_result($result,$i,"r_to"); $r_od=mysql_result($result,$i,"r_od"); $r_dd=mysql_result($result,$i,"r_dd"); $r_pd=mysql_result($result,$i,"r_pd"); $r_td=mysql_result($result,$i,"r_td"); } if(($i % 2)==0) { $bgcolor="FFFFFF"; }else{ $bgcolor="EEEEEE"; } if ($tid == 0) { $table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td><a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name</a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr> "; } else { if ($p_ord > 959) { $table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td>(<a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name)*</a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr> "; } elseif ($r_bird == 0) { $table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td><i><a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name</i></a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr> "; } else if ($fayr == "" OR $yearoffreeagency == $fayr) { $table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td><a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name</a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr> "; } } $i++; } $table_ratings=$table_ratings." </tbody> </table> "; if ($tid != 0) { $table_totals=$table_totals." <table class=\"sortable\"> <thead> <tr bgcolor=$color1><th><font color=$color2>Pos</font></th><td colspan=3><font color=$color2>Player</font></th><th><font color=$color2>g</font></th><th><font color=$color2>gs</font></th><th><font color=$color2>min</font></th><th><font color=$color2>fgm</font></th><th><font color=$color2>fga</font></th><th><font color=$color2>ftm</font></th><th><font color=$color2>fta</font></th><th><font color=$color2>3gm</font></th><th><font color=$color2>3ga</font></th><th><font color=$color2>orb</font></th><th><font color=$color2>reb</font></th><th><font color=$color2>ast</font></th><th><font color=$color2>stl</font></th><th><font color=$color2>to</font></th><th><font color=$color2>blk</font></th><th><font color=$color2>pf</font></th><th><font color=$color2>pts</font></th></tr> </thead> <tbody> "; Any ideas? Hi, I need javascript to sort as windows sorts its file. For eg: When the files name contains special characters, number and alphabets(#@!$%.doc, test1.doc,test2.doc, 1.doc) javascript sorts alphabets first and then special characters, but windows sorts special characters first and then alphabets in ascending. I want javascript to sort my array as windows. Kindly help me out in this case. Thanks, Deva. Hello, I have a portfolio online (boctay.com/asaf) and I'm using the quicksand plugin with some sorting/filtering script. My question is simple, for some reason when clicking on one of the buttons to filter, the items are first ordered into a vertical row then redrawn horizontally. Never seen this before and have no clue what is causing it. Thanks for any help. Much appreciated. I am trying to make an addition to a .js plugin for a wiki. Currently it does not support sorting my IP address. So far I have created the below code which does more than the script initially did, but is still not sorting 100% correctly. Any tips? Code: sort_ipaddr: function(a,b){ aa = a[0].split(".",4); bb = b[0].split(".",4); var counti = 0; for (var i=0; i<4; i++) { if (parseInt(parseFloat(aa[i])) == parseInt(parseFloat(bb[i]))){} if (parseInt(parseFloat(aa[i])) < parseInt(parseFloat(bb[i]))){counti--} if (parseInt(parseFloat(aa[i])) > parseInt(parseFloat(bb[i]))){counti++} } return counti; }, EDIT I've also tried this which is closer but still not there. Code: sort_ipaddr: function(a,b){ aa = a[0].split(".",4); bb = b[0].split(".",4); var resulta = (aa[3]+(aa[2]*256)+(aa[1]*256*256)+(aa[0]*256*256*256)); var resultb = (bb[3]+(bb[2]*256)+(bb[1]*256*256)+(bb[0]*256*256*256)); return resulta-resultb; }, This results in a list like so: 10.1.15.22 10.1.16.22 10.1.15.23 10.1.15.24 10.1.16.24 10.1.15.25 What I need help with is sorting the [0] of my array. I need it to sort so that if the user puts in his last name, whether its uppercase or lowercase (Adams, adams), it will sort through the list of students I add, and put them in an alphabetic order i.e adams, Adams, cook, Douglas....and so on. Here is the code that I have below for just sorting by last name, which will seperate the lowercase entries from the uppercase entries. That would be the opposite of what I want. I had it sorted so that it would sort the [o], which is where the last name value is located. Any thoughtful help would be appreciated. Code: function Last_names( s1, s2 ) { if ( s1[0] > s2[0] ) return 1; return 0; } function sortLast(form) { StudentsLists.sort( Last_names ); windows(form);//calling the windows function each time the sortLast function is called } I am trying to sort an array of strings based on the location of a string called "this.oText.value" inside an array of strings called "aList." If "this.oText.value" comes earlier in an entry in aList (call it "a") than another entry "b", I want "a" to appear before "b". Clearly, there is something very wrong with my code. It really isn't doing anything as of right now. Code: aList.sort(sortArray); function sortArray(a,b) { if(a.toLowerCase().indexOf(this.oText.value.toLowerCase()) < b.toLowerCase().indexOf(this.oText.value.toLowerCase())) return 1; else if(a.toLowerCase().indexOf(this.oText.value.toLowerCase()) > b.toLowerCase().indexOf(this.oText.value.toLowerCase())) return -1; else return 0; } Hi Guys , I have Array of objects , where we have combinations of integers and strings and now I want sort these objects , Example of Array [ 1333, A12, b233, Abc , zaa] the output should as Abc,A12,b233,zaa,1333 I need the code snippet fot this in IE9 Browser list of country names sorting on keys. we are using Ajax javascript: Code: var data= remoteRequest(url); the data like Code: data={" 11":"Australia"," 14":"Bermuda"," CAN":"Canada"," 12":"France"," 15":"Germany"," IND":"India"," 16":"Russia"," 13":"South Africa"," 10":"UK"," USA":"United States"} name_set= eval('('+data+')'); it fine in all browser the array like Code: name_set[11]=Australia name_set[14]=Bermuda name_set[CAN]=Canada name_set[12]=France name_set[15]=Germany name_set[IND]=India name_set[16]=Russia name_set[13]=South Africa name_set[10]=UK name_set[USA]=United States but in the IE9 browser array keys sorting as like. Code: name_set[10]=UK name_set[11]=Australia name_set[12]=France name_set[13]=South Africa name_set[14]=Bermuda name_set[15]=Germany name_set[16]=Russia name_set[CAN]=Canada name_set[IND]=India name_set[USA]=United States so please help me, how to stop the array sorting in IE9. thanks in Advance, Venu Hello, I have an Object, containing an object, containing an object....etc. So, it's an Object of objects. Not an Array of objects. And now I want to sort this Object of objects. Is there a "native" way in JavaScript to sort an object? Or do I understand it correctly, that I first have to convert the Object to an Array, and than perform an array.sort ? This question is just for my understanding of JavaScript, so I don't go into the wrong direction. Tnx in advance for any explanation. Say I have a gallery of 16 images and I have a menu for the gallery to sort the images by Most Viewed, Most Recent, Highest Rating, A-Z, Z-A.. if I were to click 1 of the menu links it will change the order of the images but is it possible to actually show the images MOVING into their new places? Looking for a jquery solution if possible.. if not still open to anythin that would work Hi All, I've done a few searches on it, but I can't seem to find the correct order for my date / time script. I have a field that returns the date/time in the following format: Y-m-d H:i:s (i.e. yyyy-mm-dd hh:mm:ss) What I want to do is rearrange this so that it says: 'dd' 'month-name' 'yyyy' at 'time' Anyone have any ideas? Thanks, Neil So, I'm using code like the following and getting errors: result = a list of cars with each property separated by ',' and each car separated by '|' This code creates and populates the array (seems to work) Code: var carList = new Array(); var cars = new Array(); var properties = new Array(); cars = result.split("|"); for(var i=0; i<cars.length;i++){ properties = cars[i].split(","); carlist[carlist.length++] = new car(items[0],items[1],items[2]); function car(id,make,model){ carID = id; carMake = make; carModel = model; } Later, I want to sort the array and call it like this: carList.sort(carSort); here's the sort code Code: function carSort(a, b){ var x = a.carMake.toLowerCase(); var y = b.carMake.toLowerCase(); return ((x < y) ? -1 : ((x > y) ? 1 : 0)); } I get the error that "a.carMake does not exist" (in firebug). What am I doing wrong? Hi guys, I am having a problem where the sorting function I have designed is supposed to sort an array of records into descending order in terms of each record's "score" value, I would expect the below code to alert 2,1,0 in that order but it's as if the code didn't even sort it. Help would greatly be appreciated Code: var g=[{sco 0,index:0},{sco 2,index:1},{sco 1,index:2}] function sortScores(scoreRecs){ var swapped; do for(i=0;i<scoreRecs.length-1;i++){ if(scoreRecs[i].score<scoreRecs[i+1].score){ var a=scoreRecs[i] var b=scoreRecs[i+1] var c=a; a=b; b=c; swapped=true; } else{ swapped=false; } } while(swapped==true) } sortScores(g); alert(g[0].score); alert(g[1].score); alert(g[2].score); Hi All. I have a dynamic Coldfusion form which adds multiple addresses which holds fields like Town, Postcode, ResidentFromDate, ResidentToDate... and on click of the save button I have a script function that runs which I am trying add all the elements of this form into an array and then sort by ResidentFromDate and output the results... Here is what I have at the minute but my sort does not seem to be working and is throwing out results in the following order (10/2006, 01/2005, 08/2006, 01/2007) and that is clearing not in descending order... If someone could take a look at my script and tell me what is wrong I would be much appreciated (see below) * I seem to be getting results and it is just not sorting correctly, so my theory is something is up with the part where I am calling addresses.sort. Code: var dates = { convert:function(d) { // Converts the date in d to a date-object. The input can be: // a date object: returned without modification // an array : Interpreted as [year,month,day]. NOTE: month is 0-11. // a number : Interpreted as number of milliseconds // since 1 Jan 1970 (a timestamp) // a string : Any format supported by the javascript engine, like // "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc. // an object : Interpreted as an object with year, month and date // attributes. **NOTE** month is 0-11. return ( d.constructor === Date ? d : d.constructor === Array ? new Date(d[0],d[1],d[2]) : d.constructor === Number ? new Date(d) : d.constructor === String ? new Date(d) : typeof d === "object" ? new Date(d.year,d.month,d.date) : NaN ); }, compa function(a,b) { // Compare two dates (could be of any type supported by the convert // function above) and returns: // -1 : if a < b // 0 : if a = b // 1 : if a > b // NaN : if a or b is an illegal date // NOTE: The code inside isFinite does an assignment (=). return ( isFinite(a=this.convert(a).valueOf()) && isFinite(b=this.convert(b).valueOf()) ? (a>b)-(a<b) : NaN ); }, inRange:function(d,start,end) { // Checks if date in d is between dates in start and end. // Returns a boolean or NaN: // true : if d is between start and end (inclusive) // false : if d is before start or after end // NaN : if one or more of the dates is illegal. // NOTE: The code inside isFinite does an assignment (=). return ( isFinite(d=this.convert(d).valueOf()) && isFinite(start=this.convert(start).valueOf()) && isFinite(end=this.convert(end).valueOf()) ? start <= d && d <= end : NaN ); } } // define array var addresses = []; // if atAddrSince is greater than 5 years from today if (atAddrSince > addrFromValidate) { // loop round each previous address for (i=1;i<=maxID;i++) { var addrPart = []; if (eval('document.fmAddPrevAddr.address1'+i)) { var address1 = eval('document.fmAddPrevAddr.address1'+i+'.value'); var town = eval('document.fmAddPrevAddr.town'+i+'.value'); var country = eval('document.fmAddPrevAddr.country'+i+'.value'); var postcode = eval('document.fmAddPrevAddr.postcode'+i+'.value'); var residentfrom = eval('document.fmAddPrevAddr.ataddrsince'+i+'.value'); var residentto = eval('document.fmAddPrevAddr.ataddrto'+i+'.value'); // insert into array addrPart[0]=address1; addrPart[1]=town; addrPart[2]=country; addrPart[3]=postcode; addrPart[4]=residentfromdate; addrPart[5]=residenttodate; addresses.push(addrPart); } } addresses.sort(sortfunction) function sortfunction(a, b) { if(dates.compare(a[4],b[4]) == -1) { return true; } else { return false; } } for (var v=0;v<addresses.length;v++) { var addresses1 = addresses[v]; alert(addresses1[4]); } } Many Thanks, George |