[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 ...
随机推荐
- IDEA中添加各种依赖pom.xml文件内容
刚实习的小白,今天准备进入项目,纳尼,前辈把框架什么的都搭建好了,默默的抹了一把辛酸泪,刚刚接触自学框架的时候,添加依赖的时候总是各种问题,让前辈发给我之后,才发现人家写的代码相当优美了.下面就是前辈 ...
- 【USACO 2.1.3】三值的排序
[题目描述] 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排 ...
- ext.apply和ext.applyIf
apply的用法: Ext中apply及applyIf方法的应用 apply及applyIf方法都是用于实现把一个对象中的属性应用于另外一个对象中,相当于属性拷贝. 不同的是apply将会覆盖目标对象 ...
- MVC中的控制器
authour: chenboyi updatetime: 2015-04-25 12:22:35 friendly link: 目录: 1,思维导图 2,控制器约定 3,action向视图传值的 ...
- PHP之路——验证码实现
验证码生成并保存到session <?php //开始session session_start(); //画布大小 $image = imagecreate(100, 40); $color ...
- Spring Test 整合 JUnit 4 使用
这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了.因为所有的Bean都需要在applicationContext.xml中加载好,之后再通过@Resource去取得.如果每次 ...
- 打印机PCL漏洞原理分析
0x01 漏洞概要 PCL代表打印机控制语言(Printer Control Language),由惠普公司开发,并被广泛使用的一种打印机协议.关于另一种页面描述语言,应该提一提由Adobe设计的Po ...
- 转:开源项目学习方法ABC
文章来自于 http://yizhaolingyan.net/?p=123#comment-207 学习各种开源项目,已经成为很多朋友不可回避的工作内容了.笔者本人也是如此.在接触并学习了若干个开源项 ...
- Mesh绘制雷达图(UGUI)
参考资料:http://www.cnblogs.com/jeason1997/p/5130413.html ** 描述:雷达图 刷新 radarDate.SetVerticesDirty(); usi ...
- 基于Karma和Jasmine的AngularJS测试
1:工程目录结构 y@y:karma-t01$ tree -L 3.├── client│ ├── app│ │ └── user│ ├── bower_components│ │ ...