当前位置:网站首页>sort排序
sort排序
2022-07-27 20:39:00 【yibucuo】
arr.sort()不传参数,排纯字符串时
var arr = ['a', 'cc', 'ef', 'cb', 'ca', 'ea', 'bc']
arr.sort()
console.log(arr); // ['a', 'bc', 'ca', 'cb', 'cc', 'ea', 'ef']
brr.sort()不传参数,排纯数字时
var brr = [10, 5, 2, 8, 1, 100, 30, 12, 4, 3]
brr.sort()
console.log(brr);// [1, 10, 100, 12, 2, 3, 30, 4, 5, 8]
crr.sort()不传参数,数字和字符串混合排序
var crr = ['a', 10, 'cc', 5, 'ef', 'cb', 2, 'ca', 8, 'ea', 1, 'bc', 100,'a1','c1', 30, 12, 4, 3]
crr.sort()
console.log(crr);// [1, 10, 100, 12, 2, 3, 30, 4, 5, 8, 'a', 'a1', 'bc', 'c1', 'ca', 'cb', 'cc', 'ea', 'ef']
drr.sort((a, b) => a - b)
var drr = [10, 5, 2, 8, 1, 100, 30, 12, 4, 3]
drr.sort((a, b) => a - b)
console.log(drr);// [1, 2, 3, 4, 5, 8, 10, 12, 30, 100]
err.sort((a, b) => a.first_name.localeCompare(b.first_name))
var err = [
{
first_name: 'a', last_name: 'a_a' },
{
first_name: 'cc', last_name: 'cc_cc' },
{
first_name: 'ef', last_name: 'ef_ef' },
{
first_name: 'cb', last_name: 'cb_cb' },
{
first_name: 'ca', last_name: 'ca_ca' },
{
first_name: 'ea', last_name: 'ea_ea' },
{
first_name: 'bc', last_name: 'bc_bc' },
];
err.sort((a, b) => a.first_name.localeCompare(b.first_name))
console.log(err);
[{
…}, {
…}, {
…}, {
…}, {
…}, {
…}, {
…}]
0: {
first_name: 'a', last_name: 'a_a'}
1: {
first_name: 'bc', last_name: 'bc_bc'}
2: {
first_name: 'ca', last_name: 'ca_ca'}
3: {
first_name: 'cb', last_name: 'cb_cb'}
4: {
first_name: 'cc', last_name: 'cc_cc'}
5: {
first_name: 'ea', last_name: 'ea_ea'}
6: {
first_name: 'ef', last_name: 'ef_ef'}
length: 7
总体代码如下:
<!DOCTYPE html>
<html lang="en">
<body>
<script> var arr = ['a', 'cc', 'ef', 'cb', 'ca', 'ea', 'bc'] arr.sort() console.log(arr); // ['a', 'bc', 'ca', 'cb', 'cc', 'ea', 'ef'] </script>
<script> var brr = [10, 5, 2, 8, 1, 100, 30, 12, 4, 3] brr.sort() console.log(brr);// [1, 10, 100, 12, 2, 3, 30, 4, 5, 8] </script>
<script> var crr = [10, 5, 2, 8, 1, 100, 30, 12, 4, 3] crr.sort((a, b) => a - b) console.log(crr);// [1, 2, 3, 4, 5, 8, 10, 12, 30, 100] </script>
<script> var drr = [ {
first_name: 'a', last_name: 'a_a' }, {
first_name: 'cc', last_name: 'cc_cc' }, {
first_name: 'ef', last_name: 'ef_ef' }, {
first_name: 'cb', last_name: 'cb_cb' }, {
first_name: 'ca', last_name: 'ca_ca' }, {
first_name: 'ea', last_name: 'ea_ea' }, {
first_name: 'bc', last_name: 'bc_bc' }, ]; drr.sort((a, b) => a.first_name.localeCompare(b.first_name)) console.log(drr);// </script>
</body>
</html>
边栏推荐
- 怎么使用C# Winform实现复制文件显示进度
- Deploy dolphin scheduler high availability cluster based on rainbow
- What is the MySQL data storage method?
- Solve the problem that the last bit of IP address access is odd and even, or even and odd (the problem encountered when the cloud encryption machine connects to the cloud server, the whole process is
- helm chart详解及常用命令:helm template / package / plugin
- Desai wisdom number - other charts (parallel coordinate chart): family's willingness to allocate assets in the future
- Implicit indicators for evaluating the advantages and disadvantages of automated testing
- Tips and extensions of graph theory
- Tita 的OKR系统与其他同类型产品,或者是共享文档等相比,有什么优势?
- 简单实用的数据可视化案例
猜你喜欢

Pro multi store version system, versatile is it!

Introduction to the paper | distributed graph simulation

MySQL basic installation and startup

如何快捷地查看H.265视频播放器EasyPlayer的API属性及其使用方法?

Object creation process and object layout

NDK series (6): let's talk about the way and time to register JNI functions

Vulnhub range double trouble

【图像检测】基于Combined Separability Filter实现鼻孔和瞳孔等圆检测matlab源码
![[elm classification] classification of UCI data sets based on nuclear limit learning machine and limit learning machine, with matlab code](/img/bd/fe105f0af3a24ad6f220fa35bb5e8d.png)
[elm classification] classification of UCI data sets based on nuclear limit learning machine and limit learning machine, with matlab code

Solve the problem that the last bit of IP address access is odd and even, or even and odd (the problem encountered when the cloud encryption machine connects to the cloud server, the whole process is
随机推荐
华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
Www 2019 | Han: heterograph attention network
8000 word explanation of OBSA principle and application practice
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
Promise solves asynchrony
Interviewer: I can't carry a backpack at all. Are you going by yourself or I'll give you a lift?
如何快捷地查看H.265视频播放器EasyPlayer的API属性及其使用方法?
Convnext:a convnet for the 2020s - model Brief
初步了解Panda3D音频和高级交互组件
一篇文章读懂人工神经网络
许锦波:AI蛋白质预测与设计
Introduction to the paper | language model for long text under transformer architecture
【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码
Trends in software development in 2022
【CVA估值训练营】如何快速读懂上市公司年报——第四讲
[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code
编辑复制粘贴判定问题(bug?),所见即所得显示符号问题反馈。
Solve the problem that the last bit of IP address access is odd and even, or even and odd (the problem encountered when the cloud encryption machine connects to the cloud server, the whole process is
What are the advantages of Tita's OKR system over other similar products or shared documents?
Simple and practical data visualization cases