[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. ...
随机推荐
- shell脚本中使用echo显示带颜色的内容
shell脚本中使用echo显示带颜色的内容,需要使用参数-e 格式如下: echo -e "\033[字背景颜色;文字颜色m字符串\033[0m" 例如: echo -e &qu ...
- shell-code-6-输入输出重定向
解释: 1. 文件描述符0通常是标准输入(STDIN,终端),1 是标准输出(STDOUT,终端),2 是标准错误输出(STDERR). 2. 如果希望 stderr 追加到 file 文件末尾,可以 ...
- JavaScript正则表达式-字符类
字符列表 在方括号内指定一个或者多个字符组成的字符列表,与字符列表中任意字符匹配,都被认为是匹配的.每次匹配只能匹配列表中的一个字符. str = "bird,head,fed,meadow ...
- debian 中的jdk
1 openjdk 2 debian安装java的脚本 3 下载bin自己编译
- Delphi第三方控件安装卸载指南
基本安装1.对于单个控件,Componet-->install component..-->PAS或DCU文件-->install; 2.对于带*.dpk文件的控件包,File--& ...
- Python (Page Object实例)
Page Object是Selenium自动化测试项目开发实践的最佳设计模式之一,通过对界面元素和功能模块的封装减少冗余代码,同时在后期维护中,若元素定位或功能模块发生变化,只需要调整页面元素或功能模 ...
- OSPF 一 基础
本节介绍ospf路由选择协议 为链路状态 路由选择协议 一 分类 open shortest path first 开放最短路优先 公有协议 单区域的ospf实施 运行在一个自治系 ...
- vim使用技巧二 模式
第一部分模式 第2章 普通模式 打开vim的默认状态即为普通模式 普通模式的命令强大 很大程度源于可以把操作符与动作命令结合在一起 技巧7 停顿时请移开画笔 工欲善其事,必先利其器 准 ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- 分析nginx日志常用的命令总结
1. 利用grep ,wc命令统计某个请求或字符串出现的次数 比如统计GET /app/kevinContent接口在某天的调用次数,则可以使用如下命令: cat /usr/local/nginx/l ...