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

JavaScript - Simple Array Sort

aalli

Champion
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
180
Likes
137
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Simple Array Sort using JavaScript. This code will automatically sort the array table when the user select a value from an option. The code use onclick() function to initiate a method that sort an array by getting the option value and compare in switch() in order to determine the value to be sorted, after that the array list will be undergoing in conditional statement to organize the index position by the use of sortArray(). You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it.

We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Simple Array Sort</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <form

    >
  18. <div

    class

    =

    "form-inline"

    >
  19. <label

    >

    Sort key</

    label

    >
  20. <select

    id

    =

    "key"

    class

    =

    "form-control"

    >
  21. <option

    value

    =

    "firstname"

    >

    Firstname</

    option

    >
  22. <option

    value

    =

    "lastname"

    >

    Lastname</

    option

    >
  23. <option

    value

    =

    "address"

    >

    Address</

    option

    >
  24. </

    select

    >
  25. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "sort();"

    >

    Sort</

    button

    >
  26. </

    div

    >
  27. </

    form

    >
  28. <br

    /

    >
  29. <table

    class

    =

    "table table-bordered"

    >
  30. <thead

    class

    =

    "alert-info"

    >
  31. <tr

    >
  32. <td

    >

    Firstname</

    td

    >
  33. <td

    >

    Lastname</

    td

    >
  34. <td

    >

    Address</

    td

    >
  35. </

    tr

    >
  36. </

    thead

    >
  37. <tbody

    id

    =

    "result"

    style

    =

    "background-color:#fff;"

    ></

    tbody

    >
  38. </

    table

    >
  39. </

    div

    >
  40. </

    body

    >
  41. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  42. </

    html

    >

Creating the Script

This code contains the script of the application. This code will sort the array when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var

    members =

    [
  2. {

    "firstname"

    :

    "John"

    ,

    "lastname"

    :

    "Smith"

    ,

    "address"

    :

    "Tokyo"

    }

    ,
  3. {

    "firstname"

    :

    "Claire"

    ,

    "lastname"

    :

    "Temple"

    ,

    "address"

    :

    "Racoon City"

    }

    ,
  4. {

    "firstname"

    :

    "Natasha"

    ,

    "lastname"

    :

    "Romanof"

    ,

    "address"

    :

    "Nazi"

    }
  5. ]

    ;


  6. displayData(

    )

    ;

  7. function

    displayData(

    )

    {
  8. var

    data =

    ""

    ;
  9. if

    (

    members.length

    >

    0

    )

    {
  10. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  11. data +=

    "<tr>"

    ;
  12. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  13. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  14. data +=

    "<td>"

    +

    members[

    i]

    .address

    +

    "</td>"

    ;
  15. data +=

    "</tr>"

    ;
  16. }
  17. }

  18. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  19. }

  20. function

    sort(

    )

    {
  21. var

    val=

    document.getElementById

    (

    'key'

    )

    .value

    ;

  22. switch

    (

    val)

    {
  23. case

    "firstname"

    :
  24. if

    (

    members.length

    >

    0

    )

    {
  25. var

    sortArray =

    members;

  26. sortArray.sort

    (

    function

    (

    a,

    b)

    {
  27. if

    (

    a.firstname

    <

    b.firstname

    )

    {
  28. return

    -

    1

    ;
  29. }

  30. if

    (

    a.firstname

    >

    b.firstname

    )

    {
  31. return

    1

    ;
  32. }

  33. return

    0

    ;
  34. }

    )

    ;


  35. var

    data =

    ""

    ;

  36. if

    (

    members.length

    >

    0

    )

    {
  37. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  38. data +=

    "<tr>"

    ;
  39. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  40. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  41. data +=

    "<td>"

    +

    members[

    i]

    .address

    +

    "</td>"

    ;
  42. data +=

    "</tr>"

    ;
  43. }
  44. }

  45. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  46. }
  47. break

    ;
  48. case

    "lastname"

    :
  49. if

    (

    members.length

    >

    0

    )

    {
  50. var

    sortArray =

    members;

  51. sortArray.sort

    (

    function

    (

    a,

    b)

    {
  52. if

    (

    a.lastname

    <

    b.lastname

    )

    {
  53. return

    -

    1

    ;
  54. }

  55. if

    (

    a.lastname

    >

    b.lastname

    )

    {
  56. return

    1

    ;
  57. }

  58. return

    0

    ;
  59. }

    )

    ;


  60. var

    data =

    ""

    ;

  61. if

    (

    members.length

    >

    0

    )

    {
  62. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  63. data +=

    "<tr>"

    ;
  64. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  65. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  66. data +=

    "<td>"

    +

    members[

    i]

    .address

    +

    "</td>"

    ;
  67. data +=

    "</tr>"

    ;
  68. }
  69. }

  70. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  71. }
  72. break

    ;
  73. case

    "address"

    :
  74. if

    (

    members.length

    >

    0

    )

    {
  75. var

    sortArray =

    members;

  76. sortArray.sort

    (

    function

    (

    a,

    b)

    {
  77. if

    (

    a.address

    <

    b.address

    )

    {
  78. return

    -

    1

    ;
  79. }

  80. if

    (

    a.address

    >

    b.address

    )

    {
  81. return

    1

    ;
  82. }

  83. return

    0

    ;
  84. }

    )

    ;


  85. var

    data =

    ""

    ;

  86. if

    (

    members.length

    >

    0

    )

    {
  87. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  88. data +=

    "<tr>"

    ;
  89. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  90. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  91. data +=

    "<td>"

    +

    members[

    i]

    .address

    +

    "</td>"

    ;
  92. data +=

    "</tr>"

    ;
  93. }
  94. }

  95. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  96. }
  97. break

    ;
  98. }
  99. }

There you have it we successfully created a Simple Array Sort using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,340

326,200

326,208

Top