Sort can automatically arrange items in an array. In this lesson we look at the basics including how to sort an array of strings alphabetically and the correct way to perform a numerical sort on an array of numbers. We finish as always with a practical use-case that shows not only sort in action, but also how it can be chained together with other array methods such as map and join.

Problem with sort number in Array:

var data = [10, 20, 15];
var sorted = data.sort(); console.log(sorted); // [10, 15, 20]

Code works fine, but there is a problem:

When calling sort on an array with numbers, what actually happens is each number is converted to a string, and they're compared using their position in Unicode.

var data = [10, 20, 15, 30 ,2];
var sorted = data.sort(); console.log(sorted); // [10, 15, 2, 20, 30]

This is because, in Unicode, 10 does come before 2. Again, this is because these numbers are being converted to strings first.

To solve the problem:


var data = [10, 20, 15, 30 ,2];
var sorted = data.sort( (a,b)=>{
return a-b;
} ); console.log(sorted);

We need to provide a comparative function to make it work. If a - b < 0 then it just put a before b;

Also according to that, we can compare the lenght of string, then sort them in order:

var sorted = names.sort( (a,b)=>{
return a.length - b.length;
} );
console.log(sorted); // ["Bob", "Kent", "Kettly", "Jusnine"]

A more useful example : sort objects

var list = lessons.sort( (a,b)=>{
return a.views-b.views
} )
.map( (lession)=>{
return ` <li>${lession.title} - ${lession.views}</li>`;
} )
.join('\n'); var output = `<ul>\n${list}\n</ul>`; console.log(output);
/*
"<ul>
<li>Javascript Array methods in depth - concat - 1000</li>
<li>Javascript Array methods in depth - join - 1025</li>
<li>Javascript Array methods in depth - slice - 1050</li>
</ul>"
*/

[Javascript ] Array methods in depth - sort的更多相关文章

  1. [Javascript] Array methods in depth - filter

    Array filter creates a new array with all elements that pass the test implemented by the provided fu ...

  2. [Javascript] Array methods in depth - slice

    Array slice creates a shallow copy of an array. In this lesson we cover, in detail, exactly what a ' ...

  3. [Javascript] JavaScript Array Methods in Depth - push

    Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...

  4. [Javascript] Array methods in depth - indexOf

    indexOf is used to search for a value or reference inside of an array. In this lesson we first look ...

  5. [Javascript] Array methods in depth - some

    some returns a boolean value after passing each item in the source array through the test function t ...

  6. JavaScript Array methods performance compare

    JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...

  7. javascript Array Methods(学习笔记)

    ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach();  2.map();  3.filter();  4.every();  5.some();  6.reduce() ...

  8. JavaScript Array 对象

    JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...

  9. JavaScript Array(数组)对象

    一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...

随机推荐

  1. 为什么z-index不起作用

    感觉很简单的东西 在用的时候 可能会遇到这样活那样的问题 这就是要注意细节:参考地址:http://www.ourjour.com/136/ 设置z-index 不起作用,可能是这三个原因: 1.父标 ...

  2. Linux svn直接删除版本库文件

    业务目录:/home/web/oa.youxi.com/htdocs/materialsvn地址:svn://192.168.13.61:/oa.youxi.com/htdocs/material m ...

  3. 《编写高质量代码--Web前端开发修炼之道》读书笔记

    前言 这两周参加公司的新项目,采用封闭式开发(项目成员在会议室里开发),晚上加班到很晚,所以没时间和精力写原创博客了,今天就分享下这篇<编写高质量代码--Web前端开发修炼之道>读书笔记吧 ...

  4. rhel-server-6.2-i386安装gcc、g++步骤

    安装的版本:rhel-server-6.2-i386 RHEL 6.2默认是没有gcc和gcc-c++环境的,而且我也没有$购买正版服务.只能本地安装了,总结方法如下: 上传安装镜像rhel-serv ...

  5. 跟我学android-使用Eclipse开发第一个Android应用(三)

    打开Eclipse,选择 File—New –Android Application Project Application Name  就是我们的 应用名称,也是我们在手机应用程序列表里看到的名称. ...

  6. Cloud Foundry中 JasperReports service集成

    Cloud Foundry作为业界第一个开源的PaaS解决方案,正越来越多的被业界接受和认可.随着PaaS的发展,Cloud Foundry顺应潮流,充分发挥开源项目的特点,到目前为止,已经支持了大批 ...

  7. jQuery 做好七件事帮你提升jQuery的性能

    1. Append Outside of Loops 凡是触及到DOM都是有代价的.如果你向DOM当中附加大量的元素,你会想一次性将它们全部附加进来,而不是分多次进行.当在循环当中附加元素就会产生一个 ...

  8. 安装KornShell(KSH)

    Korn shell 是一个unix上的shell 程序,主要用在各种unix系统上,比如:sun/oracle unix,AIX等.ksh是有贝尔实验室的David korn开发出来的,ksh结合了 ...

  9. Hazelcase 简介

    原博客地址:http://blog.csdn.net/zhu_tianwei/article/details/47984599 Hazelcast是一种内存数据网格in-memory data gri ...

  10. Jedis 一

    //连接Jedis public Jedis getJedis(){ Jedis jedis = new Jedis("192.168.1.12",6379); //权限认证 // ...