MrAndy
SEO Innovator
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.
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.
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
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.
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- </
head
>
- <body
>
- <nav class
=
"navbar navbar-default"
>
- <div
class
=
"container-fluid"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
Sort Array Index By Gender Using JavaScript</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <div
class
=
"col-md-2"
></
div
>
- <div
class
=
"col-md-8"
>
- <button
type
=
"button"
class
=
"btn btn-primary"
onclick
=
"sortAscending()"
>
Ascending</
button
>
<button
type
=
"button"
class
=
"btn btn-primary"
onclick
=
"sortDescending()"
>
Descending</
button
>
- <br
/
><br
/
>
- <table
class
=
"table table-bordered"
>
- <thead
class
=
"alert-info"
>
- <tr
>
- <td
>
Firstname</
td
>
- <td
>
Lastname</
td
>
- <td
>
Gender</
td
>
- </
tr
>
- </
thead
>
- <tbody
id
=
"result"
style
=
"background-color:#fff;"
></
tbody
>
- </
table
>
- </
div
>
- </
div
>
- </
body
>
- <script
src
=
"js/script.js"
></
script
>
- </
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.
- var
members =
[
- {
"firstname"
:
"John"
,
"lastname"
:
"Smith"
,
"gender"
:
"Male"
}
,
- {
"firstname"
:
"Claire"
,
"lastname"
:
"Temple"
,
"gender"
:
"Female"
}
,
- {
"firstname"
:
"Hailee"
,
"lastname"
:
"Stenfield"
,
"gender"
:
"Female"
}
,
- ]
;
- displayData(
)
;
- function
displayData(
)
{
- var
data =
""
;
- if
(
members.length
>
0
)
{
- for
(
var
i=
0
;
i <
members.length
;
i++
)
{
- data +=
"<tr>"
;
- data +=
"<td>"
+
members[
i]
.firstname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.lastname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.gender
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- function
sortAscending(
)
{
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.gender
<
b.gender
)
{
- return
-
1
;
- }
- if
(
a.gender
>
b.gender
)
{
- return
1
;
- }
- return
0
;
- }
)
;
- var
data =
""
;
- if
(
members.length
>
0
)
{
- for
(
var
i=
0
;
i <
members.length
;
i++
)
{
- data +=
"<tr>"
;
- data +=
"<td>"
+
members[
i]
.firstname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.lastname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.gender
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- }
- function
sortDescending(
)
{
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.gender
>
b.gender
)
{
- return
-
1
;
- }
- else
if
(
a.gender
<
b.gender
)
{
- return
1
;
- }
else
{
- return
0
;
- }
- }
)
;
- var
data =
""
;
- if
(
members.length
>
0
)
{
- for
(
var
i=
0
;
i <
members.length
;
i++
)
{
- data +=
"<tr>"
;
- data +=
"<td>"
+
members[
i]
.firstname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.lastname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.gender
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- }
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.