• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

JavaScript - Sorting Array Table By Lastname

PdrFrncsc

Signal Intelligence Expert
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
112
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
In this tutorial we will create a Sorting Array Table By Lastname using JavaScript. This code will automatically sort the array table when the user click a button. The code use onclick() function to initiate a method that sort an array by base on the click button then it will be undergoing in a conditional statement that organize the index position of an array 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 - Sorting Array Table By Lastname</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  18. <div

    class

    =

    "col-md-8"

    >
  19. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "sortAscending()"

    >

    Ascending</

    button

    >

    <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "sortDescending()"

    >

    Descending</

    button

    >

  20. <br

    /

    ><br

    /

    >

  21. <table

    class

    =

    "table table-bordered"

    >
  22. <thead

    class

    =

    "alert-info"

    >
  23. <tr

    >
  24. <td

    >

    Firstname</

    td

    >
  25. <td

    >

    Lastname </

    td

    >
  26. </

    tr

    >
  27. </

    thead

    >
  28. <tbody

    id

    =

    "result"

    style

    =

    "background-color:#fff;"

    ></

    tbody

    >
  29. </

    table

    >
  30. </

    div

    >
  31. </

    div

    >
  32. </

    body

    >
  33. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  34. </

    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"

    }

    ,
  3. {

    "firstname"

    :

    "Claire"

    ,

    "lastname"

    :

    "Temple"

    }

    ,
  4. {

    "firstname"

    :

    "Steve"

    ,

    "lastname"

    :

    "Roger"

    }

    ,
  5. {

    "firstname"

    :

    "Paul"

    ,

    "lastname"

    :

    "Miles"

    }

    ,
  6. ]

    ;

  7. displayData(

    )

    ;

  8. function

    displayData(

    )

    {
  9. var

    data =

    ""

    ;
  10. if

    (

    members.length

    >

    0

    )

    {
  11. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  12. data +=

    "<tr>"

    ;
  13. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  14. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  15. data +=

    "</tr>"

    ;
  16. }
  17. }

  18. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  19. }


  20. function

    sortAscending(

    )

    {
  21. if

    (

    members.length

    >

    0

    )

    {
  22. var

    sortArray =

    members;

  23. sortArray.sort

    (

    function

    (

    a,

    b)

    {
  24. if

    (

    a.lastname

    <

    b.lastname

    )

    {
  25. return

    -

    1

    ;
  26. }

  27. if

    (

    a.lastname

    >

    b.lastname

    )

    {
  28. return

    1

    ;
  29. }

  30. return

    0

    ;
  31. }

    )

    ;


  32. var

    data =

    ""

    ;

  33. if

    (

    members.length

    >

    0

    )

    {
  34. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  35. data +=

    "<tr>"

    ;
  36. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  37. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  38. data +=

    "</tr>"

    ;
  39. }
  40. }

  41. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  42. }

  43. }

  44. function

    sortDescending(

    )

    {
  45. if

    (

    members.length

    >

    0

    )

    {
  46. var

    sortArray =

    members;

  47. sortArray.sort

    (

    function

    (

    a,

    b)

    {
  48. if

    (

    a.lastname

    >

    b.lastname

    )

    {
  49. return

    -

    1

    ;
  50. }

  51. else

    if

    (

    a.lastname

    <

    b.lastname

    )

    {
  52. return

    1

    ;
  53. }

    else

    {
  54. return

    0

    ;
  55. }


  56. }

    )

    ;


  57. var

    data =

    ""

    ;

  58. if

    (

    members.length

    >

    0

    )

    {
  59. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  60. data +=

    "<tr>"

    ;
  61. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  62. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  63. data +=

    "</tr>"

    ;
  64. }
  65. }

  66. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  67. }
  68. }

There you have it we successfully created a Sorting Array Table By Lastname 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.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,513

356,536

356,563

Top
Raidforums