[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. ...
随机推荐
- PWA天气应用
https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0 1.介绍 这里将使用PWA技术来构建一个天气web应用,这个ap ...
- 二段式fsm
1.推荐在敏感表下的默认状态为X,这样描述的好处有2个: 好处1:仿真易观察bug. 好处2:综合对不定态X的处理是"Don't Care",即任何没有定义的状态寄存器向量都会被忽 ...
- Web框架之Django_02基本操作(Django项目启动配置、数据库连接、orm、增删改查)
摘要: Django项目简单现实过程 pycharm连接数据库 Django之orm简单操作增删改查 一.新建Django项目.配置.设置: 新建Django项目:(为了熟悉Django操作,暂时全部 ...
- Windows Server 2008 R2+SQL Server 2014 R2升级到Windows Server 2016+SQL Server 2016
环境: 操作系统:Windows Server 2008 R2 数据库:SQL Server 2014 因SQL Server 2016可以无域创建AlwaysOn集群,集群只剩下单节点也不会挂掉,故 ...
- 使用selenium和phantomJS浏览器获取网页内容的小演示
# 使用selenium和phantomJS浏览器获取网页内容的小演示 # 导入包 from selenium import webdriver # 使用selenium库里的webdriver方法调 ...
- The North American Invitational Programming Contest 2018 H. Recovery
Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...
- bounds 和frame区别
仔细看下这个图就知道了
- .NET重构(三):在注册和充值中,触发器的使用
导读:机房做到注册和充值了,有两个关键点:在注册的时候,同时给该用户写入充值记录:在充值的时候,给该用户更改余额信息.第一次做的时候,是一条一条的写,那时候师傅就说了触发器和存储过程的使用,现在终于用 ...
- Python logging 学习
基本用法: import logging #初始化logger 对象 logger = logging.getLogger("main") #设置logger对象基础级别,后面的h ...
- spring之lazy-init
lazy-init:延迟实例化 ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化.提前实例化意味着作为初始化过程的一部分,appli ...