473,569 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Number s and Words Sorter</title>
</head>
<body>
<td><b>Sortin g Machine</b></td><p>

<script type="text/javascript">

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

function sortvalues(para m)
{
var inputvalues =
document.Form.t extInput.value. toUpperCase().s plit(" ")

if (param==0)

inputvalues.sor t()

else

inputvalues.sor t(sortalpha)

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

document.Form.t extOutput.value = document.Form.t extOutput.value
+inputvalues[i] + " "

document.Form.t extOutput.value += inputvalues[inputvalues.len gth-1]

}
</script>

<form name="Form">

<textarea rows="10" name="textInput " cols="30" wrap="virtual">
</textarea>
<textarea rows="10" name="textOutpu t" cols="30" wrap="virtual">
</textarea>
<p>
<input type = "button" value="Sort Letters" name="Button"
onClick="sortva lues(0)">
<br>
<input type = "button" value="Sort Numbers" name="Button"
onClick="sortva lues(1)">
<br>
<input type = file name ="file" size =15>
<br>
<input type = "button" value="print" name="Button"
onClick="print( document.Form.t extOutput.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 2988
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>Number s and Words Sorter</title</head>
<body>
<td><b>Sortin g 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(para m)
{
var inputvalues =
document.Form.t extInput.value. toUpperCase().s plit(" ")
This particular split leaves EOL (end of line) markers in place.
>
if (param==0)

inputvalues.sor t()

else

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

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

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

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

<form name="Form">

<textarea rows="10" name="textInput " cols="30"
wrap="virtual">
</textarea>
<textarea rows="10" name="textOutpu t" cols="30"
wrap="virtual">
</textarea>
<p>
<input type = "button" value="Sort Letters" name="Button"
onClick="sortva lues(0)">
<br>
<input type = "button" value="Sort Numbers" name="Button"
onClick="sortva lues(1)">
<br>
<input type = file name ="file" size =15<br>
<input type = "button" value="print" name="Button"
onClick="print( document.Form.t extOutput.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
17599
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 array_reverse(). Can anyone tell me why?
4
8171
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 float elements. Thanks in advance
18
2439
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 = { { 4.0, 2.0 }, { 5.0, 8.0 }, { 6.0, 6.0 },
0
1691
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 listbox/flexgrid? Example: OrderMatrix(0, iSortTheseRows) Where "0" is the sortable value (articlenumber), and "iSortTheseRows" are
21
3179
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a...
4
3024
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. example array: arr
20
34725
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 when it gets to the end of a line in the array. It also has to run several times to get the values it keeps in completely correct order. Here's...
152
9768
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 = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
5
5256
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 seem to be an easy way to do this. I have found implementations that use the non-generic IComparer interface but it seems to me to be more elegant to...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.