aalli
Champion
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.
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
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
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.
- <!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"
>
JavaScript - Simple Array Sort</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <form
>
- <div
class
=
"form-inline"
>
- <label
>
Sort key</
label
>
- <select
id
=
"key"
class
=
"form-control"
>
- <option
value
=
"firstname"
>
Firstname</
option
>
- <option
value
=
"lastname"
>
Lastname</
option
>
- <option
value
=
"address"
>
Address</
option
>
- </
select
>
- <button
type
=
"button"
class
=
"btn btn-primary"
onclick
=
"sort();"
>
Sort</
button
>
- </
div
>
- </
form
>
- <br
/
>
- <table
class
=
"table table-bordered"
>
- <thead
class
=
"alert-info"
>
- <tr
>
- <td
>
Firstname</
td
>
- <td
>
Lastname</
td
>
- <td
>
Address</
td
>
- </
tr
>
- </
thead
>
- <tbody
id
=
"result"
style
=
"background-color:#fff;"
></
tbody
>
- </
table
>
- </
div
>
- </
body
>
- <script
src
=
"js/script.js"
></
script
>
- </
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
- var
members =
[
- {
"firstname"
:
"John"
,
"lastname"
:
"Smith"
,
"address"
:
"Tokyo"
}
,
- {
"firstname"
:
"Claire"
,
"lastname"
:
"Temple"
,
"address"
:
"Racoon City"
}
,
- {
"firstname"
:
"Natasha"
,
"lastname"
:
"Romanof"
,
"address"
:
"Nazi"
}
- ]
;
- 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]
.address
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- function
sort(
)
{
- var
val=
document.getElementById
(
'key'
)
.value
;
- switch
(
val)
{
- case
"firstname"
:
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.firstname
<
b.firstname
)
{
- return
-
1
;
- }
- if
(
a.firstname
>
b.firstname
)
{
- 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]
.address
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- break
;
- case
"lastname"
:
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.lastname
<
b.lastname
)
{
- return
-
1
;
- }
- if
(
a.lastname
>
b.lastname
)
{
- 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]
.address
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- break
;
- case
"address"
:
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.address
<
b.address
)
{
- return
-
1
;
- }
- if
(
a.address
>
b.address
)
{
- 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]
.address
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- break
;
- }
- }
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.