473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

2 dimensional array - sorting mechanism

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
1 2975
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
0
by: Sick | last post by:
Hey, The Array.Sort function only applies to one-dimension arrays. Is there a solution for sorting a two-dimensional array, preferably *without* the usage of additional controls such as...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
20
by: Acolyte | last post by:
Hey, I'm working on a project that involves sorting a two-dimensional array of integers using bubble sort. I've got it semi-working, my only issue is that it seems to be 'losing' values, I'm assuming...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.