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

sorting a textarea.

P: n/a
Hi all,
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 tex area or
data file and show the result in the 2nd text area, in other words,
howerever the words are set, horizontally the one following the other
or vertically, 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,

Example : say i have " to be or not to be,this is the question" i want
to have "BE BE,THIS IS NOT OR QUESTION THE TO TO" this seems correct
but actually it is not because the "," ascii is less than the letters
so it should go first, and the second problem is that there are spaces
in the output that i can not understand where they are coming from and
i dont know how to deal with them.

the second problem is that if i enter:
to
be
or
not
to
be,
this
is
the
question

pressing enter after each word the script doesnt seem to work at all
it just copy the input to the output, instead i want to make it do the
following :
",
BE
BE
THIS
IS
NOT
OR
QUESTION
THE
TO
TO
"

here is my code:

<html>
<head>
<title>Numbers and Words Sorter</title>
</head>
<body>
<b>Sorting Machine</b><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!
Nov 2 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Totti wrote:
I am a newbie to javascript learning it for 2 months now,
You don't have to mention that, it is all-too-obvious. There is no
"javascript", BTW.
i am trying to make a sorter who will read english words from the one tex
area or data file and show the result in the 2nd text area, in other
words, [one incredibly long, nested, unfinished all-lowercase sentence
snipped]
You are not nearly clever enough to do that or understand solutions
presented to that end here yet. The sooner you get that, the sooner
you can stop whining about your 2-month-newbie status in each of your
postings and start developing software for a change.

<http://catb.org/~esr/faqs/smart-questions.html>
Score adjusted

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Nov 2 '08 #2

P: n/a
OHHH einstein is back,
he is judging people smartness!!!

Hey cool guy act like a pro. not like a kiddy,
if you wana help then do so if not just forget about the thread and
click the X on the upper left of your screen to close and go to
another one. without a lot of blablabla... for god's sake
Nov 2 '08 #3

P: n/a
Totti wrote:
OHHH einstein is back,
While I do appreciate the sentiment, I can understand that for someone with
an intellect like a mothball, it must seem like any other person is a genius.
he is judging people smartness!!!
I am judging your smartness, or rather the demonstrated lack thereof.
Hey cool guy act like a pro. not like a kiddy,
Says the script-kiddy.
if you wana help then do so if not just forget about the thread and
click the X on the upper left of your screen to close
There is no X on the upper left on my screen. So much for your competence.
and go to another one.
Or you could simply FOAD. Usenet is not a right, and if you cannot
behave you will get no help. No competent one at least.
without a lot of blablabla... for god's sake
Says the person who turns out to be a master of blablabla.
PointedEars, f1
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 2 '08 #4

P: n/a
Totti <sa******************@gmail.comwrites:
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 tex area or
data file and show the result in the 2nd text area, in other words,
howerever the words are set, horizontally the one following the other
or vertically, the one under the other.
...
Example : say i have " to be or not to be,this is the question" i want
to have "BE BE,THIS IS NOT OR QUESTION THE TO TO" this seems correct
but actually it is not because the "," ascii is less than the letters
so it should go first,
That's not what you said above. There you said that you wanted to sort
words. A comma is not a word. I.e., you need to be more precise about
the problem you are trying to solve.

I'll assume for now that you want to split the text into sequences of
letters or puntuation and sort these alphabetically.
and the second problem is that there are spaces
in the output that i can not understand where they are coming from and
i dont know how to deal with them.
Ok, so you don't want spaces in the input to be represented in the
output at all.
the second problem is that if i enter:
to
be
....
>
pressing enter after each word the script doesnt seem to work at all
it just copy the input to the output, instead i want to make it do the
following :
....
Yes, this is what your code is doing.
>
here is my code:
....
<script type="text/javascript">
function sortalpha(x,y) {
return(x-y)
}
The name suggests that this compares alphabetically, while it actually
tries to convert the elements to numbers and compare them numerically.
The problem is that non-numbers will all be converted to NaN, which
is not a valid return value from a comparison function, so you need to
be sure that it's only used on numbers.
function sortvalues(param) {
var inputvalues =
document.Form.textInput.value.toUpperCase().split( " ")
Here you split the text on a single space. It doesn't split on
newlines (your problem from above), and it doesn't split between
punctuation and letters, and it doesn't handle multiple consequtive
spaces

Try:
var text = document.forms['Form'].elements['textInput'].value;
var inputvalues = text.toUpperCase().split(/\s+|\s*\b\s*/);

This will split between letters and punctuation or at any sequence
of whitespaces.
if (param==0) {
inputvalues.sort() }
else
inputvalues.sort(sortalpha)
What *is* sortalpha supposed to do? The default behavior of the sort
method is to sort the array's elements alphabetically as strings.
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] }
}
Here you could use the join method:

document.forms['Form'].elements['textOutput'].value =
inputvalues.join(" ");
In short: Before solving a problem, make sure that you have the
problem specified exactly. Then check that the functions you use
actually do what you want.

/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 2 '08 #5

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
There is no "javascript", BTW.
Sure there is. The word is in common use. You might not agree with its
use, but it's there.
You are not nearly clever enough to do that or understand solutions
presented to that end here yet. The sooner you get that, the sooner
you can stop whining about your 2-month-newbie status in each of your
postings and start developing software for a change.

<http://catb.org/~esr/faqs/smart-questions.html>
While ESR has some good points about dealing with the anti-social
behavior of geeks, it should not be seen as an endorsement or
recommendation of such behavior.

/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 2 '08 #6

P: n/a
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>There is no "javascript", BTW.

Sure there is. The word is in common use.
You might not agree with its use, but it's there.
That incompetent people, and sometimes even people competent in other areas,
call anything anything is no proof that a designation is a correct one.
>You are not nearly clever enough to do that or understand solutions
presented to that end here yet. The sooner you get that, the sooner
you can stop whining about your 2-month-newbie status in each of your
postings and start developing software for a change.

<http://catb.org/~esr/faqs/smart-questions.html>

While ESR has some good points about dealing with the anti-social
behavior of geeks, it should not be seen as an endorsement or
recommendation of such behavior.
You miss the point.
PointedEars
Nov 3 '08 #7

P: n/a
Lasse, Thanks a lot.
you pointed out many misunderstood concepts, and made me think twice
about what i am doing. the "(/\s+|\s*\b\s*/);" actually kind of solved
the problem.
there is something else if you please help with, if the input is a
series of words entered vertically, with your "split(/\s+|\s*\b\s*/);"
the output is arranged, but horizontally. how can i make my script
arrange horizontal input horizontally and vertical input vertically?
it should be somewhere in the way the script writes the output,

should i put like an if statement that will check for the "enter" in
the input and if it finds it write out vertically , else horizontally?
Nov 3 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.