• We just launched and are currently in beta. Join us as we build and grow the community.

List Manipulator in Javascript

zebu

Automated Testing Specialist
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
69
Likes
147
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Introduction:
This tutorial is on how to create a simple array re-orderer in Javascript.

HTML:
We are going to have a few HTML elements, here are the details...
Textarea, named area, to contain the original list.
Button, to reverse the list.
Button, to sort the list a-z.
Button, to sort the list z-a.
Button, to randomise the list.

  1. <html

    >
  2. <head

    >

  3. </

    head

    >
  4. <body

    >
  5. <textarea

    cols

    =

    '20'

    rows

    =

    '40'

    id

    =

    'area'

    ></

    textarea

    >
  6. <button

    onclick

    =

    'reverseList();'

    >

    Reverse</

    button

    >
  7. <button

    onclick

    =

    'sortList();'

    >

    Sort A-Z</

    button

    >
  8. <button

    onclick

    =

    'sortReverse();'

    >

    Sort Z-A</

    button

    >
  9. <button

    onclick

    =

    'randomise();'

    >

    Randomise</

    button

    >
  10. </

    body

    >
  11. </

    html

    >

Reverse:
As you can see from the HTML buttons, the reverseList button runs the javascript function 'reverseList' when it is clicked ('onclicked')...

  1. <

    script>
  2. function

    reverseList(

    )
  3. {
  4. var

    lines =

    document.getElementById

    (

    "area"

    )

    .value

    .split

    (

    '\n

    '

    )

    ;

    //Get all lines from 'area'.
  5. lines =

    lines.reverse

    (

    )

    ;
  6. document.getElementById

    (

    'area'

    )

    .value

    =

    ''

    ;
  7. for

    (

    var

    i=

    0

    ;

    i<

    lines.length

    ;

    i++

    )

    {
  8. var

    line =

    lines[

    i]

    ;
  9. document.getElementById

    (

    'area'

    )

    .value

    +=

    line +

    '\n

    '

    ;
  10. }
  11. }
  12. </

    script>

We simply get all the lines from the textarea 'area' and put them in to a new variable/array 'lines'. We then reverse the array, reset the textareas value to nothing and then add each line in 'lines' to the 'area' textarea.

Sort A-Z:
For sort a-z we do a similar thing to 'reverseList' except instead of using 'reverse', we use 'sort'...

  1. function

    sortList(

    )
  2. {
  3. var

    lines =

    document.getElementById

    (

    "area"

    )

    .value

    .split

    (

    '\n

    '

    )

    ;

    //Get all lines from 'area'.
  4. lines =

    lines.sort

    (

    )

    ;
  5. document.getElementById

    (

    'area'

    )

    .value

    =

    ''

    ;
  6. for

    (

    var

    i=

    0

    ;

    i<

    lines.length

    ;

    i++

    )

    {
  7. var

    line =

    lines[

    i]

    ;
  8. document.getElementById

    (

    'area'

    )

    .value

    +=

    line +

    '\n

    '

    ;
  9. }
  10. }

Sort Z-A:
Now we are able to combine the previous two examples, reverse and sort a-z, to perform a z-a sort. First we sort the list of lines named 'lines' to a-z format. Then we reverse it to get a z-a format...

  1. function

    sortReverse(

    )

    {
  2. var

    lines =

    document.getElementById

    (

    "area"

    )

    .value

    .split

    (

    '\n

    '

    )

    ;

    //Get all lines from 'area'.
  3. lines =

    lines.sort

    (

    )

    ;
  4. lines =

    lines.reverse

    (

    )

    ;
  5. document.getElementById

    (

    'area'

    )

    .value

    =

    ''

    ;
  6. for

    (

    var

    i=

    0

    ;

    i<

    lines.length

    ;

    i++

    )

    {
  7. var

    line =

    lines[

    i]

    ;
  8. document.getElementById

    (

    'area'

    )

    .value

    +=

    line +

    '\n

    '

    ;
  9. }
  10. }

Finished!
Here is the full souce...

  1. <

    html>
  2. <

    head>
  3. <

    script>
  4. function

    reverseList(

    )
  5. {
  6. var

    lines =

    document.getElementById

    (

    "area"

    )

    .value

    .split

    (

    '\n

    '

    )

    ;

    //Get all lines from 'area'.
  7. lines =

    lines.reverse

    (

    )

    ;
  8. document.getElementById

    (

    'area'

    )

    .value

    =

    ''

    ;
  9. for

    (

    var

    i=

    0

    ;

    i<

    lines.length

    ;

    i++

    )

    {
  10. var

    line =

    lines[

    i]

    ;
  11. document.getElementById

    (

    'area'

    )

    .value

    +=

    line +

    '\n

    '

    ;
  12. }
  13. }

  14. function

    sortList(

    )
  15. {
  16. var

    lines =

    document.getElementById

    (

    "area"

    )

    .value

    .split

    (

    '\n

    '

    )

    ;

    //Get all lines from 'area'.
  17. lines =

    lines.sort

    (

    )

    ;
  18. document.getElementById

    (

    'area'

    )

    .value

    =

    ''

    ;
  19. for

    (

    var

    i=

    0

    ;

    i<

    lines.length

    ;

    i++

    )

    {
  20. var

    line =

    lines[

    i]

    ;
  21. document.getElementById

    (

    'area'

    )

    .value

    +=

    line +

    '\n

    '

    ;
  22. }
  23. }

  24. function

    sortReverse(

    )
  25. {
  26. var

    lines =

    document.getElementById

    (

    "area"

    )

    .value

    .split

    (

    '\n

    '

    )

    ;

    //Get all lines from 'area'.
  27. lines =

    lines.sort

    (

    )

    ;
  28. lines =

    lines.reverse

    (

    )

    ;
  29. document.getElementById

    (

    'area'

    )

    .value

    =

    ''

    ;
  30. for

    (

    var

    i=

    0

    ;

    i<

    lines.length

    ;

    i++

    )

    {
  31. var

    line =

    lines[

    i]

    ;
  32. document.getElementById

    (

    'area'

    )

    .value

    +=

    line +

    '\n

    '

    ;
  33. }
  34. }
  35. </

    script>
  36. </

    head>
  37. <

    body>
  38. <

    textarea cols=

    '20'

    rows=

    '40'

    id=

    'area'

    ></

    textarea>
  39. <

    button onclick=

    'reverseList()'

    >

    Reverse</

    button>
  40. <

    button onclick=

    'sortList()'

    >

    Sort A-

    Z</

    button>
  41. <

    button onclick=

    'sortReverse();'

    >

    Sort Z-

    A</

    button>
  42. </

    body>
  43. </

    html>

 

452,292

324,125

324,133

Top