...

View Full Version : hash array to index then sort!



Boxhead
04-26-2004, 12:01 PM
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;
}

glenngv
04-26-2004, 12:08 PM
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.

Boxhead
04-26-2004, 12:34 PM
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]);
}
}
}

glenngv
04-26-2004, 12:44 PM
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

:confused:

Boxhead
04-26-2004, 12:54 PM
option 2.

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

cheers

glenngv
04-27-2004, 03:50 AM
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.

Boxhead
04-27-2004, 12:26 PM
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? :D 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;
}

glenngv
04-27-2004, 12:35 PM
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. :rolleyes:

Boxhead
04-27-2004, 02:10 PM
oops - took this out cos i thought you didnt need to see it!! :o

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;
}
}

Boxhead
04-27-2004, 03:41 PM
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! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum