By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

2 dimensional array - sorting mechanism

P: n/a
Hi everyone,
I am a newbie to javascript learning it for 2 months now, i am trying
to make a sorter who will read english words from the one textbox or
datafile and store them in a 2 dimensional array, so in other words,
howerever the words are set, horizontally the one following th eother
or the one under the other i want it to work, i tried some code but i
am having problems it only work when the text entered is horizontal
like the one word following the other when i try to enter a second
word after pressing ENTER, i dont get any result,

here is my code:

<html>
<head>
<title>Numbers and Words Sorter</title>
</head>
<body>
<td><b>Sorting Machine</b></td><p>

<script type="text/javascript">

function sortalpha(x,y)
{
return(x-y)
}

function sortvalues(param)
{
var inputvalues =
document.Form.textInput.value.toUpperCase().split( " ")

if (param==0)

inputvalues.sort()

else

inputvalues.sort(sortalpha)

for (i=0; i < inputvalues.length-1; i++)

document.Form.textOutput.value = document.Form.textOutput.value
+inputvalues[i] + " "

document.Form.textOutput.value += inputvalues[inputvalues.length-1]

}
</script>

<form name="Form">

<textarea rows="10" name="textInput" cols="30" wrap="virtual">
</textarea>
<textarea rows="10" name="textOutput" cols="30" wrap="virtual">
</textarea>
<p>
<input type = "button" value="Sort Letters" name="Button"
onClick="sortvalues(0)">
<br>
<input type = "button" value="Sort Numbers" name="Button"
onClick="sortvalues(1)">
<br>
<input type = file name ="file" size =15>
<br>
<input type = "button" value="print" name="Button"
onClick="print(document.Form.textOutput.value)">
<br>
<input type = "reset" value="Reset" name="Button">
</form>
</body>
</html>
================================================== =============

this code works in some cases but in others it doesnt for example in
the case of parenthesis, the parenthesis do not go before the letters
eventhough their ASCII is < , and it generates empty spaces in the
output which i can not explain, i know it is something wrong in my
code but i need your help please pointing out the problem.

I want to make this program work perfect, and i want to add more
things to it, but for the time being this is the obvious problem i am
infront,

Thanks in advance for any help!
Oct 30 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Thu, 30 Oct 2008 05:53:12 -0700, Totti wrote:
Hi everyone,
I am a newbie to javascript learning it for 2 months now, i am trying to
make a sorter who will read english words from the one textbox or
datafile and store them in a 2 dimensional array, so in other words,
howerever the words are set, horizontally the one following th eother or
the one under the other i want it to work, i tried some code but i am
having problems it only work when the text entered is horizontal like
the one word following the other when i try to enter a second word after
pressing ENTER, i dont get any result,
I'm not exactly sure what your code is trying to do. If you could post
your input and the EXPECTED output, that might help.

Meanwhile, here are a few pointers on things that looked strange to me.

<html>
<head>
<title>Numbers and Words Sorter</title</head>
<body>
<td><b>Sorting Machine</b></td><p>
(Bad markup, you should remove the "<td></td>")
<script type="text/javascript">

function sortalpha(x,y)
{
return(x-y)
This is a numeric test. With strings it ALWAYS returns the value NaN.
(You should be using compare operators here.)
}

function sortvalues(param)
{
var inputvalues =
document.Form.textInput.value.toUpperCase().split( " ")
This particular split leaves EOL (end of line) markers in place.
>
if (param==0)

inputvalues.sort()

else

inputvalues.sort(sortalpha)
I strongly suggest you put braces here. Not putting them in tends to
bite you later on.

Like this:
if (param==0) {
inputvalues.sort();
} else {
inputvalues.sort(sortalpha);
}

>
for (i=0; i < inputvalues.length-1; i++)

document.Form.textOutput.value =
document.Form.textOutput.value
+inputvalues[i] + " "
Likewise, I could include braces around this one-statement block as a
matter of style.
>
document.Form.textOutput.value += inputvalues
[inputvalues.length-1]
>
}
</script>

<form name="Form">

<textarea rows="10" name="textInput" cols="30"
wrap="virtual">
</textarea>
<textarea rows="10" name="textOutput" cols="30"
wrap="virtual">
</textarea>
<p>
<input type = "button" value="Sort Letters" name="Button"
onClick="sortvalues(0)">
<br>
<input type = "button" value="Sort Numbers" name="Button"
onClick="sortvalues(1)">
<br>
<input type = file name ="file" size =15<br>
<input type = "button" value="print" name="Button"
onClick="print(document.Form.textOutput.value)">
<br>
<input type = "reset" value="Reset" name="Button">
</form>
</body>
</html>
================================================== =============

Thanks in advance for any help!
In the future, PLEASE post any example code indented with SPACES and not
tabs.

While it doesn't have anything to do with your problem, as far as I can
tell, your HTML markup needs some help as well. Forms should have (and I
believe) are required to have an action. It is seldom a good idea to
give multiple form entries the same name.
Oct 30 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.