Solved

sorting a list of decimal numbers in a textarea

Posted on 2004-09-28
4
254 Views
Last Modified: 2006-11-17
Hi!
I want to display a text area and have the user input a series of numbers of value less than one( ie .0129) When a button is pressed, a function will return the sorted list to the textarea. Can this be done? I know javascript has a sort() function that works on an array, but how do you get the lines of the text area into an array?
Thanks,
Annas
0
Comment
Question by:annas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:monosodiumg
ID: 12168536
If you can get the user to separate the values using a delimiter string e.g. "," or " " then you can get them into an array like this:
var sNumbers = MyTextArea.value
var aNumbers = sNumbers.split(",")

Now you can sort your array. Be aware the the default compare function used by the sort method  will sort in alphabetical order, which works if all your numbers start with a  decimal point but not otherwise. E.g. 11 comes before 2 because "11" is before "2".
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 100 total points
ID: 12169688
How about this:

<html>
<head>
<title>Q_21147659</title>
<script>
function sortArea(theBtn){
  theArea = theBtn.form.userInput;
  theArea.value = theArea.value.split("\r\n").sort(num).join("\n");
}
function num(a, b){
  a*=1; b*=1;
  if(a>b) return 1;
  if(a<b) return -1;
  return 0;
}
</script>
</head>
<body>
<form>
<textarea name="userInput" rows="20"" cols="80">
</textarea>
<input type=button value="Sort" onClick="sortArea(this)">
</form>
<body>
</html>

0
 

Author Comment

by:annas
ID: 12172288
Thanks, Zvonko, your code worked perfectly!
Annas
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12172307
You are welcome.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
[form-control] Retain values after a POST action 21 66
JS to redirect to prev page 8 40
Diff of the day 2 35
Angular2 remove duplicates 11 19
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question