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的更多相关文章

  1. javascript 一些函数的实现 Function.prototype.bind, Array.prototype.map

    * Function.prototype.bind Function.prototype.bind = function() { var self = this, context = [].shift ...

  2. javascript & global event & custom event

    javascript & global event & custom event new CustomEvent object let event = new CustomEvent( ...

  3. Javascript自执行匿名函数(function() { })()的原理浅析

    匿名函数就是没有函数名的函数.这篇文章主要介绍了Javascript自执行匿名函数(function() { })()的原理浅析的相关资料,需要的朋友可以参考下 函数是JavaScript中最灵活的一 ...

  4. JavaScript学习总结(十五)——Function类

    在JavaScript中,函数其实是对象,每个函数都是Function类的实例,既然函数对象,那么就具有自己的属性和方法,因此,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. 一.函数的 ...

  5. .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 ...

  6. [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 ...

  7. (JavaScript基础向)sort()方法里的排序函数的理解

    比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...

  8. 791. Custom Sort String - LeetCode

    Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...

  9. Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)

    英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...

随机推荐

  1. Day11名称空间,作用域,闭包函数

    Day11 1.函数对象: ①可以被引用 ​ ②可以作为另一个函数的参数 ​ ③可以作为另一个函数的返回值0 ​ ④可以被存储到容器类型中 2.函数嵌套: ①嵌套调用:在一个函数中调用了另一个函数 ​ ...

  2. Django项目 生成依赖包

    生成依赖包文件 requirements.txt,最好用 pip3, 如果有 python2 的话会默认使用 py2 的 pip pip3 -r freeze > requirements.tx ...

  3. Django 千锋培训的学习笔记(2)

    Django 千锋培训读书笔记 https://www.bilibili.com/video/av17879644/?p=1 切换到创建项目的目录 cd C:\Users\admin\Desktop\ ...

  4. Hi3519V101 SDK安装以及开发环境搭建

    Hi3519V101 Linux开发环境 1.安装Hi3519V101 SDKHi3519V101 SDK是基于Hi3519V101 DMEB的软件开发包,包含了在Linux相关应用开发时使用的各种工 ...

  5. flask中的上下文_请求上下文和应用上下文

    前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...

  6. Django之ORM操作(重要)

    Django ORM操作 一般操作 看专业的官网文档,做专业的程序员! 必知必会13条   <1> all(): 查询所有结果 <2> get(**kwargs): 返回与所给 ...

  7. 2,格式化输出and编码基础

    格式化输出: input格式化输出: name = input('请输入姓名:') name = input('请输入姓名:') Age = int(input('请输入年龄:')) job = in ...

  8. hdu2087

    #include <stdio.h> #include <string.h> int main(){ int cnt,i,j,k; +],tmp[+]; int strl,tm ...

  9. Maven部署异常:on project standalone-pom: Cannot deploy artifact from the local repository解决方法

    MAVEN部署异常 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache. ...

  10. 【Luogu】P1013进制位(搜索)

    题目链接在这里 这题和虫食算比较类似.做完这道题可以去做虫食算.都是搜索一类的题. 这样 我们分析题目可以发现进制只可能是字母的个数,也就是n-1.为什么? 因为题目要求完整的加法表才算数.如果进制低 ...