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

Sort Array Index By Gender Using JavaScript

MrAndy

SEO Innovator
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
155
Likes
30
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial will teach you how to do Sort Array Index By Gender using JavaScript. The program can sort a certain array object by adding a condition in the index position. This is a free code and you can apply this to your working projects. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, 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"

    >

    Sort Array Index By Gender Using JavaScript</

    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. <td

    >

    Gender</

    td

    >
  27. </

    tr

    >
  28. </

    thead

    >
  29. <tbody

    id

    =

    "result"

    style

    =

    "background-color:#fff;"

    ></

    tbody

    >
  30. </

    table

    >
  31. </

    div

    >
  32. </

    div

    >
  33. </

    body

    >
  34. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  35. </

    html

    >

Creating the Script

This code contains the script of the application. The code will sort the array object by gender when user click the button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var

    members =

    [
  2. {

    "firstname"

    :

    "John"

    ,

    "lastname"

    :

    "Smith"

    ,

    "gender"

    :

    "Male"

    }

    ,
  3. {

    "firstname"

    :

    "Claire"

    ,

    "lastname"

    :

    "Temple"

    ,

    "gender"

    :

    "Female"

    }

    ,
  4. {

    "firstname"

    :

    "Hailee"

    ,

    "lastname"

    :

    "Stenfield"

    ,

    "gender"

    :

    "Female"

    }

    ,
  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]

    .gender

    +

    "</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.gender

    <

    b.gender

    )

    {
  25. return

    -

    1

    ;
  26. }

  27. if

    (

    a.gender

    >

    b.gender

    )

    {
  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 +=

    "<td>"

    +

    members[

    i]

    .gender

    +

    "</td>"

    ;
  39. data +=

    "</tr>"

    ;
  40. }
  41. }

  42. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  43. }

  44. }

  45. function

    sortDescending(

    )

    {
  46. if

    (

    members.length

    >

    0

    )

    {
  47. var

    sortArray =

    members;

  48. sortArray.sort

    (

    function

    (

    a,

    b)

    {
  49. if

    (

    a.gender

    >

    b.gender

    )

    {
  50. return

    -

    1

    ;
  51. }

  52. else

    if

    (

    a.gender

    <

    b.gender

    )

    {
  53. return

    1

    ;
  54. }

    else

    {
  55. return

    0

    ;
  56. }


  57. }

    )

    ;


  58. var

    data =

    ""

    ;

  59. if

    (

    members.length

    >

    0

    )

    {
  60. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  61. data +=

    "<tr>"

    ;
  62. data +=

    "<td>"

    +

    members[

    i]

    .firstname

    +

    "</td>"

    ;
  63. data +=

    "<td>"

    +

    members[

    i]

    .lastname

    +

    "</td>"

    ;
  64. data +=

    "<td>"

    +

    members[

    i]

    .gender

    +

    "</td>"

    ;
  65. data +=

    "</tr>"

    ;
  66. }
  67. }

  68. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;

  69. }
  70. }

There you have it we successfully created a Sort Array Index By Gender 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,496

336,844

336,852

Top