Sort text in Javascript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <html>
  2. <head><title>Javascript Sort</title</head>
  3. <body>
  4.  
  5. <script language="javascript">
  6. function generate(s){
  7. var temp = new Array();
  8. temp = s.split('\n');
  9. temp.sort();
  10. var result = "";
  11. var i;
  12. for(i=0; i<temp.length; i++) {
  13. result += temp[i] + "\n";
  14. }
  15. return result;
  16. }
  17.  
  18. </script>
  19.  
  20. <form name="myform">
  21. <textarea name="mytext" rows=20 cols=60>
  22. Insert lines to sort here.
  23. </textarea>
  24. <br><br>
  25. <input type="button" value="Sort!" onClick="javascript: document.myform.mytext.value = generate(document.myform.mytext.value);"><br><br>
  26. </form>
  27.  
  28.  
  29.  
  30. </body>
  31. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.