[Javascript] Use a custom sort function on an Array in Javascript
Sorting in Javascript with sort
uses lexical sorting by default, which means it will sort in alphabetical order. That's fine for strings of characters, but it means that arrays of numbers don't sort in numerical order! To fix that, we'll pass a custom comparator function to sort
. The function you pass to sort
will be passed two values from the array at a time, and should return a value <0, =0 or >0, based on which value should be sorted first in the final array.
Once you have a custom sort function implemented, you can sort in any way that you'd like. We'll show that by pulling out just part of a string, and sorting based on that value.
const numberArr = [, , , -, , -] const descSort = numberArr.sort((a, b) => {
return b - a
}) console.log(descSort) const numberSorted = numberArr.sort((a, b) => {
if(a < && b < ) {
return a - b // -n should be start from small to large
} else if(a < || b < ) {
return b - a // +n come first, -n come last
} else {
return a - b // from small to large
}
}) console.log(numberSorted) // [0, 6, 12, 50, -9, -1] const floorArr = [
"6th Floor",
"2nd Floor",
"11th Floor",
"8th Floor",
"7th Floor",
"9th Floor",
"1st Floor",
"3rd Floor",
"10th Floor",
"5th Floor",
"4th Floor",
] const sorted = floorArr.sort((a, b) => {
return a.match(/\d+/) - b.match(/\d+/)
}) console.log(sorted) // ["1st Floor", "2nd Floor", "3rd Floor", "4th Floor", "5th Floor", "6th Floor", "7th Floor", "8th Floor", "9th Floor", "10th Floor", "11th Floor"] [, , , , -, -]
[Javascript] Use a custom sort function on an Array in Javascript的更多相关文章
- javascript 一些函数的实现 Function.prototype.bind, Array.prototype.map
* Function.prototype.bind Function.prototype.bind = function() { var self = this, context = [].shift ...
- javascript & global event & custom event
javascript & global event & custom event new CustomEvent object let event = new CustomEvent( ...
- Javascript自执行匿名函数(function() { })()的原理浅析
匿名函数就是没有函数名的函数.这篇文章主要介绍了Javascript自执行匿名函数(function() { })()的原理浅析的相关资料,需要的朋友可以参考下 函数是JavaScript中最灵活的一 ...
- JavaScript学习总结(十五)——Function类
在JavaScript中,函数其实是对象,每个函数都是Function类的实例,既然函数对象,那么就具有自己的属性和方法,因此,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. 一.函数的 ...
- .sort(function(a,b){return a-b});
var points = [40,100,1,5,25,10]; var b= points.sort(function(a,b){return a-b}); console.log(b); 那个fu ...
- [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce
Instead of writing complex operators, it's usually best to write simple, single-purpose operators th ...
- (JavaScript基础向)sort()方法里的排序函数的理解
比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...
- 791. Custom Sort String - LeetCode
Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...
- Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)
英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...
随机推荐
- perl的bareword
perl的bareword可能被认为:label . 句柄 .函数 . 普通字符串. 上下文不同,解释器有歧义. 最好用 use strict; use warning;
- 【mysql】 load local data infield 报错 ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql> load data local infile '/Users/flint/learn/mysql/pet' into table bx_pet; 执行报错 ERROR 1148 ( ...
- Python9-继承2-day25(大年初二)
继承:什么是什么关系组合:什么有什么关系单继承 先抽象再继承,几个类直接的相同代码抽象出来,成为父类 子类自己没有的名字可以使用父类的方法和属性 如果子类自己有,一定先用自己的 在类中使用self的时 ...
- Codeforces C. Sonya and Problem Wihtout a Legend(DP)
Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...
- Knockout v3.4.0 中文版教程-10-绑定-控制文本内容和外观-visible绑定
4.绑定 1. 控制文本内容和外观 1. visible绑定 目的 visible绑定可以根据你传入绑定的值控制关联的DOM元素显示或隐藏. 例子 <div data-bind="vi ...
- Wordpress无法上传图片
当在自己新搭建的个人网站添加文章的时候,我遇到了wordpress最普遍的第一个问题——无法上传图片. 每次图片上传完成之后跳出如上图无法建立目录的警告,根据提示首先确定是否有修改文件的权限通 ...
- 【SVN】http和https的区别
导读:输入网址的时候,经常输入http://什么什么的,但http是什么,一直都不知道.然后,这回在SVN的学习中,又出现了http和https,而且还有说https的8443端口相对优越,我就在想, ...
- 九度oj 题目1090:路径打印
题目描述: 给你一串路径,譬如:a\b\c a\d\e b\cst d\你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右缩一格,就像这样:a b c d ...
- 【Luogu】P1312Mayan游戏(暴搜)
题目链接 由于是暴搜题,所以这篇博客只讲怎么优化剪枝,以及一些细节. 模拟消除思路:因为消除可以拆分成小的横条或竖条,而这些条的长度至少为三,所以一块可消除的区域至少会有一个中心点.这里的中心点可以不 ...
- Tree 树(树形期望dp)
题意也是需要解释一下的,这个期望步数,是需要求无限步的时候的,就是你只要能到达,都要算上去, 这个我一开始真的没什么思路,打了暴力,搞一个精度,结果全超时了,看来精度定的太细了. 出题人的题解是这个, ...