[Javascript ] Array methods in depth - sort
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的更多相关文章
- [Javascript] Array methods in depth - filter
Array filter creates a new array with all elements that pass the test implemented by the provided fu ...
- [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 ' ...
- [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 ...
- [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 ...
- [Javascript] Array methods in depth - some
some returns a boolean value after passing each item in the source array through the test function t ...
- JavaScript Array methods performance compare
JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...
- javascript Array Methods(学习笔记)
ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach(); 2.map(); 3.filter(); 4.every(); 5.some(); 6.reduce() ...
- JavaScript Array 对象
JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...
- JavaScript Array(数组)对象
一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...
随机推荐
- 使用EditText搜索listview里面的内容,实现Listview跟随变动的情况
1.布局的XML文件里面添加EditText控件(省略)控件id=mSearch ListView的id=admin_lv; 2.一.获取ListView展示的数据(通过适配器获取) 二.这个是我要说 ...
- sql问题
表中某个指标重复,去掉重复项: select * from #temp where A0107 in (select A0107 from #temp group by A0107having CO ...
- [转]MySQL导入和导出SQL脚本
首先,使用mysqldump命令的前提是,在Cmd中进入mysql安装目录下的bin目录下,才可以使用该命令.我的mysql安装在E:盘,所以,首先进入bin目录下:E:/Program Files/ ...
- PHP json的插入和解析在数据库中的操作
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. PHP中往数据库中存储json数据在项目开发中也经常遇到,下面我就 ...
- 14_RHEL7安装mplayer
这里用nux-dextop仓库来配置 Nux Dextop桌面对CentOS和RHEL 6/7可用.它是一个面对CentOS.RHEL.ScientificLinux的含有许多流行的桌面和多媒体相关的 ...
- 002.AngularJs调用Restful实现CRUD
本节我们主要给大家介绍AngularJs如何调用Restful,实现数据的CRUD. 主要用到的技术: 后端:ASP.NET WebApi + SQLServer2008 前端:AngularJs,B ...
- window.onload() 等待所有的数据加载都完成之后才会触发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JDBC驱动汇总
Microsoft SQL Server (6.5, 7, 2000 and 2005) and Sybase (10, 11, 12). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- django + nginx + raspberypi + pidaro
对于一个从事后台开发的人来说,搞了个网站,我自己都觉得有点意外.不是很懂html,不是很懂css,不是很懂js,不是很懂web开发模式/框架,不是很懂httpd/nginx--,web的东西样 ...
- 读取word文件.选择了TextParse
待续! 代码还没分离出来.. 分离后会上传上来 不支持wps 文件 . ]]>