当前位置:网站首页>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>
边栏推荐
- 如何快捷地查看H.265视频播放器EasyPlayer的API属性及其使用方法?
- 初步了解Panda3D音频和高级交互组件
- 看GAN如何一步步控制图像生成风格?详解StyleGAN进化过程
- Cloudcompare & PCL platform convex hull method to calculate crown volume
- 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
- Safety Fundamentals 1
- Deploy dolphin scheduler high availability cluster based on rainbow
- CSDN dedicated killer technology -- Google browser plug-in
- Shuffle, partition and read of tfrecord
- 习题 --- BFS
猜你喜欢

Nature综述:微生物群落形成过程中的优先效应

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

51 MCU internal peripherals: real time clock (SPI)
![[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code](/img/6a/92ace81b798810299a2fec0ce6ae70.png)
[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code

怎么使用C# Winform实现复制文件显示进度

【软考软件评测师】2014综合知识历年真题

【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码

Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?

Microsoft Office 2019 download installation activation tutorial (full process diagram)

"The faster the code is written, the slower the program runs."
随机推荐
Parameter transmission of components
[number recognition] handwritten number recognition based on knowledge base with matlab code
Blender plug-in of 2022
See how Gan controls the image generation style step by step? Explain the evolution process of stylegan in detail
Security-001
【图像检测】基于Combined Separability Filter实现鼻孔和瞳孔等圆检测matlab源码
Convnext:a convnet for the 2020s - model Brief
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
Lianmai live broadcast system software - voice chat system
Bi Shi - University Logistics Management System Based on SSM
Basic lighting of unity
八大排序之冒泡、快排、堆排、基数排序
强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)
containerd ctr运行ansible容器执行ansible-playbook任务完整命令
LANproxy mapping local development environment
Kubevera deploys applications through cli
Find and leverage xxE – XML external entity injection
我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律
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
51 MCU internal peripherals: real time clock (SPI)