Jump to content

Javascript Sorting Problem.


wallyson

Recommended Posts

Ok here is my xml that im parsing using a parer:

<?xml version="1.0" encoding="UTF-8"?><!-- Edite with [somekind of software] --><Holidays>		<Data>			<Month>August</Month>			<Day>2</Day>			<Holiday>Virgin of the Angels Day</Holiday>			<Country>(CR)</Country>			<Year>2006</Year>		</Data>		<Data>			<Month>August</Month>			<Day>3</Day>			<Holiday>Fast Tishaa Beav</Holiday>			<Country>(ISR)</Country>			<Year>2006</Year>		</Data>		<Data>			<Month>August</Month>			<Day>3</Day>			<Holiday>San Salvador Day</Holiday>			<Country>(SLV)</Country>			<Year>2006</Year>		</Data>		<Data>			<Month>August</Month>			<Day>4</Day>			<Holiday>San Salvador Festival</Holiday>			<Country>(SLV)</Country>			<Year>2006</Year>		</Data>		<Data>			<Month>August</Month>			<Day>2</Day>			<Holiday>Virgin of the Angels Day</Holiday>			<Country>(IRL)</Country>			<Year>2006</Year>		</Data>					</Holidays>

Ok so its an xml file full of holidays, dates, and months. easy enoughOK when a user selects a particular Month from a drop down menu, all the holidays that are with in that month should appear. I have this part working. Now what I want to do is only display 1 instance of a holiday example: If i have 15 Christmas's in December I only want 1 Christmas to show up, not all 15 here is my code now:

function showHolidays(pass){var heys = new Array();heys = pass.split(' ');var theDiv= "results";var cats = new Array('Month','Day','Holiday','Country','Year' );var output = ' ';result = parseXML('holidaysxml.xml', true, cats, 'Month', heys[0]);output += '<p><font color="white">'+heys[0]+' '+heys[1]+'</font></p>'+'<p><font color="white"> Holidays</font></p>'+'<ul>';for (var x = 0; x < result.length; x++) {						if(result[x][0] == heys[0])			{								var holy = "'"+result[x][2]+"'";						var moly = "'"+pass+"'";						var soly = "'"+result[x][1]+"'";						var hello = "'"+result[x][3]+"'";								output +='<li style="color: white;"><a href="#" class="Select" onClick="showCountries('+holy+','+moly+','+soly+','+hello+');" >' + result[x][2] +' - '+ pass+' / ' + result[x][1]+'</a></li></div>'									 }//if}//outer foroutput += '</ul>';document.getElementById(theDiv).innerHTML = output;}

That code will simply print out every holiday in the month of whatever the user selects. I only want to print out 1 instance of each holiday if there are more than 1 holiday if they have the same name and day.Please help.Wally

Link to comment
Share on other sites

Will the file holding the data always have the days in order, or could they appear randomly?If it's in order, then you could always check [x-1] to see if it's the same as the current [x] value (if it is, don't print the date).If not, you could sort the data first then do the above, or you could hold all the dates in a temporary array as you find them, and query that list every time [x] increases?

Link to comment
Share on other sites

Will the file holding the data always have the days in order, or could they appear randomly?If it's in order, then you could always check [x-1] to see if it's the same as the current [x] value (if it is, don't print the date).If not, you could sort the data first then do the above, or you could hold all the dates in a temporary array as you find them, and query that list every time [x] increases?
Let me rework my question. To make it easier, I want to get rid of the duplicates inside my xml array. I tried using a duplicate method, but it only works on single dimentional arrays.But, I want to try making an alt. array and sorting the unique ones inside it (I guess) like you mentioned above. I may need help. I get on it really quick and then post back.
Link to comment
Share on other sites

Let me rework my question. To make it easier, I want to get rid of the duplicates inside my xml array. I tried using a duplicate method, but it only works on single dimentional arrays.But, I want to try making an alt. array and sorting the unique ones inside it (I guess) like you mentioned above. I may need help. I get on it really quick and then post back.
DOES ANYBODY KNOW HOW TO REMOVE DUPLICATES FROM AN XML JAVASCIRPT ARRAY!!! IM GOING CRAZY HERE! HA.
Link to comment
Share on other sites

Hi Wally, I'm not sure I fully understand your problem. Let me apologize up front if this is not what you need. Let me first describe what I think you have.

  • It seems that you want to avoid showing duplicate holidays in the output.
  • You would like to have the XML parser do it for you.
  • Duplicates are identified using the Holiday and Day fields.

If I'm right, this suggestion won't help you much. First, organize your loop so that it first creates a "duplicate test key"; I'll call that DTK Judging from the way you are constructing the output string, a "duplicate master list" would be easiest; I'll call that DML.Initialize your DML to empty for the start of each month's processing. Before adding to the output string and after loading up holy and soly...

  1. Construct DTK (DTK=holy+soly;).
  2. if DTK is a substring of DML skip the data
  3. if DTK is not a substring of DML, extend your output string AND append DTK to DML.

This will strip duplicates from your XML input stream regardless of sort order.

Link to comment
Share on other sites

Hi Wally, I'm not sure I fully understand your problem. Let me apologize up front if this is not what you need. Let me first describe what I think you have.
  • It seems that you want to avoid showing duplicate holidays in the output.
  • You would like to have the XML parser do it for you.
  • Duplicates are identified using the Holiday and Day fields.

If I'm right, this suggestion won't help you much. First, organize your loop so that it first creates a "duplicate test key"; I'll call that DTK Judging from the way you are constructing the output string, a "duplicate master list" would be easiest; I'll call that DML.Initialize your DML to empty for the start of each month's processing. Before adding to the output string and after loading up holy and soly...

  1. Construct DTK (DTK=holy+soly;).
  2. if DTK is a substring of DML skip the data
  3. if DTK is not a substring of DML, extend your output string AND append DTK to DML.

This will strip duplicates from your XML input stream regardless of sort order.

Hey thanks alot, I actually figured it out on my own. I ahd tried an original method that didnt work, I got so frustrated I went back to the old set up and changed things around. I ended up creating a new array and placing all the elements inside of it. Here look at it:
var temp2 = new Array();//////////////////////////////////////  This removes duplicates in the array.///////////////////////////////////for (var x = 0; x < result.length; x++) {		for(var y=0; y < result.length; y++)		{							if((result[y][2] == result[x][2]) && (result[y][1] == result[x][1]))				{						temp2[y]=result[y];						break;					}						}}

Yup thats all i did. After that I used the bubble sort to sort the array by day (1 2 3 4 5....) You get the idea.I spent a week and half trying other methods such as xsl and xpath. Man it was a simple 1 line of code.hahahha

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...