javascript根据数组以及对象数组排序sort

sort()排序数组以及数组对象

一般排序

//sort.js
var arr = ['go','javascript','java','c','python','c++','--','c+d', '5', 1, '12']
console.log(arr.sort())

$ node sort.js
[
  '--',     1,
  '12',     '5',
  'c',      'c++',
  'c+d',    'go',
  'java',   'javascript',
  'python'
]
//很明显是根据里面每一项从头到尾每一位进行排序的。这里不细究内部算法实现。

比较排序

// 比较适合排序数字,不管是字符串的形式还是数字
var arr1 = [11, 54, 34, 24, 36, 78, 12, '13']
console.log(arr1.sort((a,b) => a-b))
/*[
    11, 12, '13',
    24, 34, 36,
    54, 78
] */

var arr = ['go','javascript','java','c','python','c++','--','c+d', 11,'1c', 54, '3v',34,'3d', 24, 36, 78, 12, '13','1d']
console.log(arr.sort((a,b) => a-b))

/*
[
  'go',     'javascript',
  'java',   'c',
  'python', 'c++',
  '--',     'c+d',
  11,       '1c',
  54,       '3v',
  34,       '3d',
  12,       '13',
  24,       36,
  78,       '1d'
]
*/

根据数组中每个对象的某个key所对应的value(数字)排序

var arr = [
    {
        go: 11,
        to: 'asd'
    },
    {
        go: 54,
        to: 'asd'
    },
    {
        go: 34,
        to: 'asd'
    },
    {
        go: 24,
        to: 'asd'
    },
    {
        go: 78,
        to: 'asd'
    },
    {
        go: 12,
        to: 'asd'
    },
    {
        go: '13',
        to: 'asd'
    },
]
console.log(arr.sort((a,b) => a.go - b.go))
console.log(arr.sort((a,b) => b.go - a.go))

/*
[
  { go: 11, to: 'asd' },
  { go: 12, to: 'asd' },
  { go: '13', to: 'asd' },
  { go: 24, to: 'asd' },
  { go: 34, to: 'asd' },
  { go: 54, to: 'asd' },
  { go: 78, to: 'asd' }
]
[
  { go: 78, to: 'asd' },
  { go: 54, to: 'asd' },
  { go: 34, to: 'asd' },
  { go: 24, to: 'asd' },
  { go: '13', to: 'asd' },
  { go: 12, to: 'asd' },
  { go: 11, to: 'asd' }
]
*/

根据对象数组中某个key所对应value(字符串)的首字母大写所对应的Unicode编码排序

var arr = [
    {
        go: 'go',
        to: 'asd'
    },
    {
        go: 'javascript',
        to: 'asd'
    },
    {
        go: 'java',
        to: 'asd'
    },
    {
        go: 'c',
        to: 'asd'
    },
    {
        go: 'python',
        to: 'asd'
    },
    {
        go: 'c++',
        to: 'asd'
    },
    {
        go: '--',
        to: 'asd'
    },
    {
        go: 'c+d',
        to: 'asd'
    },
    {
        go: '1c',
        to: 'asd'
    }
]
console.log(arr.sort((a,b) => a.go[0].toUpperCase().charCodeAt() - b.go[0].toUpperCase().charCodeAt()))
console.log(arr.sort((a,b) => b.go[0].toUpperCase().charCodeAt() - a.go[0].toUpperCase().charCodeAt()))

// 只会根据第一个,第二个是不固定的
/*[
  { go: '--', to: 'asd' },
  { go: '1c', to: 'asd' },
  { go: 'c', to: 'asd' },
  { go: 'c++', to: 'asd' },
  { go: 'c+d', to: 'asd' },
  { go: 'go', to: 'asd' },
  { go: 'javascript', to: 'asd' },
  { go: 'java', to: 'asd' },
  { go: 'python', to: 'asd' }
]
[
  { go: 'python', to: 'asd' },
  { go: 'javascript', to: 'asd' },
  { go: 'java', to: 'asd' },
  { go: 'go', to: 'asd' },
  { go: 'c', to: 'asd' },
  { go: 'c++', to: 'asd' },
  { go: 'c+d', to: 'asd' },
  { go: '1c', to: 'asd' },
  { go: '--', to: 'asd' }
] */

reverse()反转数组

var arr = ['go','javascript','java','c','python','c++','--','c+d', '5', 1, '12']
console.log(arr.reverse())

$ node reverse.js 
[
  '12',     1,
  '5',      'c+d',
  '--',     'c++',
  'python', 'c',
  'java',   'javascript',
  'go'
]
// 字面意思的反转
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kboushi_UNIQUE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值