[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 ...
随机推荐
- this,super关键字的使用
this关键字 1.this是对象的别名,是当前类的实例引用 2.在类的成员方法内部使用,代替当前类的实例.在Java中,本质上是指针,相当于C++中的指针概念.如果方法中的成员在调用前没有操作实例名 ...
- iPhone真机测试Crash信息分析
一.获取Crash Log的方式 在iOS开发过程,当应用已经打包,iPhone设备通过ipa的包安装应用后,在使用过程发现crash,那么如何获取crash日志呢,现提供如下四种获取crash日志的 ...
- JavaScript那些事儿(01): 对象
一. 对象是什么 是单身童鞋们正在查找的“对象”吗?是的,他/她就是活生生的对象. Javascript是一种基于对象的语言, 你遇到的所有东西几乎都是对象. 但它又不同于基于类的语言.那么“类”又是 ...
- springmvc数组参数传递
在开发中遇到将form中的name值一样的多个input元素传递到后台,我用的是springmvc. 刚开始的时候老是报400的请求错误.后来查了下资料,其实解决方案挺简单的. 我的后台control ...
- Groovy创建和解析json
正文: 在Groovy 1.8发布新闻中,提到Groovy增加了对JSON的支持.Dustin Marx在其博文中,讲述了这一功能的使用. 用法真的很简单,创建一个JSON对象: import gr ...
- get_magic_quotes_gpc() 内置函数
get_magic_quotes_gpc()函数 在PHP中是内置的函数,这个函数的作用就是得到php.ini设置中magic_quotes_gpc选项的值. 当magic_quotes_gpc=On ...
- flvplayer.swf flv视频播放器使用方法
今天由于网页上要加入一个视频文件,就研究了一下flv视频播放器flvplayer.swf : 关于SewisePlayer 插件 演示例子链接 一.直接在html文件中加载: &l ...
- 修改PHP的默认时区
每个地区都有自己的本地时间,在网上及无线电通信中,时间的转换问题显得格外突出.整个地球分为24个时区,每个时区都有自己的本地时间.在国际无线电或网络通信场合,为了统一起见,使用一个统一的时间,成为通用 ...
- 推荐一个PHP扩展 来真正实现PHP多线程的开发
PHP扩展下载:https://github.com/krakjoe/pthreadsPHP手册文档:http://php.net/manual/zh/book.pthreads.php <?p ...
- Mahout
http://blog.csdn.net/yueyedeai/article/details/26567379