Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hash array to index then sort!

    I have a hash array of number values which I want to sort into numeric order. I understand that hash arrays can not be sorted (sorting gives the results 0,1,2,3,4....).

    I was advised by Glenngv to put the hash array into a index array then sort (http://www.codingforums.com/showthread.php?t=35545), but I am still getting 0,1,2,3,4.....

    Here is my code (arr is the hash array)

    function alldates(){
    var dates="";
    var sortarr = new Array();
    var i=0;
    for (var item in arr){
    sortarr[i]=item;
    i++;
    }
    sortarr.sort();
    for (var item in sortarr){
    if(item.length==3){
    item="0"+item;
    }
    if(dates==""){
    dates+=(item);
    }else{
    dates+=(","+item);
    }
    }
    document.form1.testing.value=dates;
    }
    Monkey

    My head hurts!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Change this line:

    sortarr[i]=item;

    to:

    sortarr[i]=arr[item];

    Change also the code that retrieves contents of sortarr array.

    What you did is you retrieve the key not its value.
    Last edited by glenngv; 04-26-2004 at 12:12 PM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cheers Glenn - I was slowly working it out!

    My only problem now is the line -

    if(sortarr[item].length==3)

    If I alert sortarr[item].length==3 it returns 'undefined' this worked on the hash array - what am i doing wrong? (ps - how clean is the code, could anything be done better?

    function alldates(){
    var dates="";
    var sortarr = new Array();
    var i=0;
    for (var item in arr){
    sortarr[i]=arr[item];
    i++;
    }
    sortarr.sort();
    for (var item in sortarr){
    if(sortarr[item].length==3){
    sortarr[item]="0"+sortarr[item];
    }
    if(dates==""){
    dates+=(sortarr[item]);
    }else{
    dates+=(","+sortarr[item]);
    }
    }
    }
    Monkey

    My head hurts!

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I really don't know what you intend it to do

    if (sortarr.length==3) //if length (number of elements) of sortarr is 3

    or

    if (sortarr[item]==3) //if value of sortarr element at index item is 3

    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    option 2.

    The values are date, which i want to be 4 digit, so I need to convert '112' to '0112'

    cheers
    Monkey

    My head hurts!

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    If the hash array elements are numbers (112) not strings ('112'), then the code should be

    if(sortarr[item].toString().length==3){...}

    But why not store items in hash array as strings that are already prefixed with zero ('0112')?

    If you put them in an index-based array and sort it, the result is still correctly sorted.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn

    cheers for your help.

    It was my original plan to use the 0 prefix, and my code is set up to do this, but I was having problems with the code removing the 0 when building the array (see thread - http://www.codingforums.com/showthread.php?t=35550)

    I never solved this problem as I couldnt get the parseInt to work, so I did it this way around. Could you help me to add the 0 prefix to the hash array to start with? Cheers

    // fills the month table with numbers
    function fill_table(month,month_length,month_date){
    day=1
    // begin the new month table
    document.write("<table class='calendar' border=1 cellspacing=1 cellpadding=0><tr>")
    document.write("<td colspan=7 align=center bgcolor='#333399'><b class='months'>"+month+" "+year+"</b></td></tr><tr bgcolor='#ff9933'>")
    // column headings
    day_title("Sun")
    day_title("Mon")
    day_title("Tue")
    day_title("Wed")
    day_title("Thu")
    day_title("Fri")
    day_title("Sat")
    document.write("</tr>")
    // pad cells before first day of month
    if(start_day!=8){
    document.write("<td colspan="+(start_day-1)+">&nbsp;</td>")
    }
    // fill the first week of days
    for (var i=start_day;i<8;i++){
    //create the date with zero at start (will alway be for first 7 days)
    caldate=month_date+day;
    if(i==1){
    document.write("<td align=center bgcolor="+endcolor+" id="+caldate+"><a href='#'; onclick='hilite("+caldate+",true);' class='cal'>"+day+"</a></td>")
    }else if(i==7){
    document.write("<td align=center bgcolor="+endcolor+" id="+caldate+"><a href='#'; onclick='hilite("+caldate+",true);' class='cal'>"+day+"</a></td>")
    }else{
    document.write("<td align=center id="+caldate+"><a href='#'; onclick='hilite("+caldate+");' class='cal'>"+day+"</a></td>")
    }
    day++
    }
    // fill the remaining weeks
    while (day <= month_length) {
    document.write("<tr>")
    for (var i=1;i<=7 && day<=month_length;i++){
    //create the date with zero at start if below 10th
    caldate=month_date+day;
    if(i==1){
    document.write("<td align=center bgcolor="+endcolor+" id="+caldate+"><a href='#'; onclick='hilite("+caldate+",true);' class='cal'>"+day+"</a></td>")//make sun coloured
    }else if(i==7){
    document.write("<td align=center bgcolor="+endcolor+" id="+caldate+"><a href='#'; onclick='hilite("+caldate+",true);' class='cal'>"+day+"</a></td>")//make sat coloured
    }else{document.write("<td align=center id="+caldate+"><a href='#'; onclick='hilite("+caldate+");' class='cal'>"+day+"</a></td>")
    }
    if(day==month_length){
    if(i<7){
    document.write("<td colspan="+(7-i)+">&nbsp;</td>")//span final cells
    }
    }
    day++
    }
    start_day=i// the first day of the next month
    document.write("</tr>")
    }
    document.write("</table>")
    }

    var arr = new Array();

    function alldates(){
    var dates="";
    var sortarr = new Array();
    var i=0;
    for (var item in arr){
    sortarr[i]=arr[item];
    i++;
    }
    sortarr.sort();
    for (var item in sortarr){
    alert(sortarr[item].length);
    if(sortarr[item].length==3){
    sortarr[item]="0"+sortarr[item];
    }
    if(dates==""){
    dates+=(sortarr[item]);
    }else{
    dates+=(","+sortarr[item]);
    }
    }
    document.form1.testing.value=dates;
    }
    Monkey

    My head hurts!

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You didn't post the code that populates the hash array arr or is it just I didn't see it in that unindented codes.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops - took this out cos i thought you didnt need to see it!!

    function hilite(cell,weekend){
    if(document.getElementById(cell).bgColor == hilitecolor && weekend){//highlighted and weekend
    document.getElementById(cell).bgColor = endcolor;
    delete arr[cell];
    }else if(document.getElementById(cell).bgColor == hilitecolor && !weekend){//highlighted and week day
    document.getElementById(cell).bgColor = "#ffffff";
    delete arr[cell];
    }else{
    document.getElementById(cell).bgColor = hilitecolor;
    arr[cell]=cell;
    }
    }
    Monkey

    My head hurts!

  • #10
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have got it all working now. I was working with UK dates so had to swop the day/month. I have also added the word 'date' to the id, as I was advised not to start id's with a number. I now need to reverse both of these so I have -

    for (var item in sortarr){
    var dateAdd = /\d+/.exec(sortarr[item])//get numbers only
    dateAdd = (dateAdd.toString().substr(2,2))+(dateAdd.toString().substr(0,2))
    if(dates==""){
    dates+=(dateAdd);
    }else{
    dates+=(","+dateAdd);
    }
    }

    what do ya think? View the whole think at www.splash-wiltshire.org.uk/date_picker_v2.asp.

    Cheers for your help

    PS could you have a look at the question I asked at the end of my last thread to you (http://www.codingforums.com/showthread.php?t=35545)- its not important, but it keeps me awake at night!
    Monkey

    My head hurts!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •