Tuesday, December 7, 2021
The Array.sort() methods sorts the array in alphabetical order by default, or you can pass in a custom sort function.
So let's say you have an array of names that's our of order that you want to sort.
If we call the names.sort() method we would get resulting array in Alphabetical order
This is fine since all the names are in lowercase letters, so what if some of the names are capitalized? What do you think will happen if you have the following array defined instead?
If we call the names.sort() method again the capitalized names will get sorted first because of precedence. Capitalize strings get sorted before lowercase strings and the resulting array will be the following.
It's not what you were expecting was it? So how would we sort the above array in the order that is alphabetical? We can do it by passing in a function that would first convert the array values into lowercase names first. Don't worry it would not change the values in the array to lowercase names, it just converts it to lowercase value in the function so that it could do the sort. So you write the sort function as the following.
names.sort(function (n1, n2) {
var a = n1.toLowerCase();
var b = n2.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
The resulting sort will output the following in the console
The code above returns a negative value if the first argument is less than the second argument, which equates to false, and returns a positive value if the first argument is greater than the second, which equates to true.
So "jason" is greater than "zack", and "adam" is greater than "zack" and so on until all the elements are sorted correctly.
Finally, when it comes to sorting numbers things can get a little confusing. Let's say you have an array of small numbers from 5 to 1
var numbers = [5,4,3,2,1];
If you call the numbers.sort() method you will get the expected result of 1 to 5.
However, in JavaScript alphabetical order means that the larger numbers gets sorted first if it's 100 or larger. So if you have the numbers array of
var numbers = [5,4,3,200,1000];
If you call the numbers.sort() method now, the number 1000 will be sorted first.
I am pretty sure that was not the result that you wanted. So how do we get the sort that we want from the smallest value to the biggest? By passing in a function into the sort() method. In the following function compares the first argument to the second argument. If it first argument should appear then return value should return a number that is negative. If the second argument should appear then the return value should be a positive number.
Or you can do the reverse with the following
So let's say you have an array of names that's our of order that you want to sort.
<script> var names = ["zack", "jim", "bob", "adam", "jason"]; </script>
If we call the names.sort() method we would get resulting array in Alphabetical order
This is fine since all the names are in lowercase letters, so what if some of the names are capitalized? What do you think will happen if you have the following array defined instead?
<script> var names = ["Zack", "jim", "bob", "adam", "Jason"]; </script>
If we call the names.sort() method again the capitalized names will get sorted first because of precedence. Capitalize strings get sorted before lowercase strings and the resulting array will be the following.
It's not what you were expecting was it? So how would we sort the above array in the order that is alphabetical? We can do it by passing in a function that would first convert the array values into lowercase names first. Don't worry it would not change the values in the array to lowercase names, it just converts it to lowercase value in the function so that it could do the sort. So you write the sort function as the following.
names.sort(function (n1, n2) {
var a = n1.toLowerCase();
var b = n2.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
The resulting sort will output the following in the console
The code above returns a negative value if the first argument is less than the second argument, which equates to false, and returns a positive value if the first argument is greater than the second, which equates to true.
So "jason" is greater than "zack", and "adam" is greater than "zack" and so on until all the elements are sorted correctly.
Finally, when it comes to sorting numbers things can get a little confusing. Let's say you have an array of small numbers from 5 to 1
var numbers = [5,4,3,2,1];
If you call the numbers.sort() method you will get the expected result of 1 to 5.
However, in JavaScript alphabetical order means that the larger numbers gets sorted first if it's 100 or larger. So if you have the numbers array of
var numbers = [5,4,3,200,1000];
If you call the numbers.sort() method now, the number 1000 will be sorted first.
I am pretty sure that was not the result that you wanted. So how do we get the sort that we want from the smallest value to the biggest? By passing in a function into the sort() method. In the following function compares the first argument to the second argument. If it first argument should appear then return value should return a number that is negative. If the second argument should appear then the return value should be a positive number.
Or you can do the reverse with the following
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
The java script is the programming language i have using these language. But today i am working with the law assignment writing service UK to the UK students those want to writing from the other sources.
ReplyDeleteHey, This is amazing I thought it's a reversal effect because my uncle is Chinese and he is working on some clinic and do some amazing facial implant surgery in Dubai by himself.
ReplyDelete