本篇参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

salesforce开发过程中,数组在前端的使用中基本是不可避免,下面的demo大家猜一下输出结果。

arraySortSample.html

<template>
<lightning-card title="integer item sort">
<ul>
<template for:each={sortedIntegerArray} for:item="integerItem">
<li key={integerItem}>
{integerItem}
</li>
</template>
</ul>
</lightning-card>
<lightning-card title="string item sort">
<ul>
<template for:each={sortedStringArray} for:item="stringItem">
<li key={stringItem}>
{stringItem}
</li>
</template>
</ul>
</lightning-card>
</template>

arraySortSample.js

import { LightningElement } from 'lwc';

export default class ArraySortSample extends LightningElement {

    integerArray = [2,5,23,4,16];

    stringArray = ['a','A','b','B'];

    get sortedIntegerArray() {
return this.integerArray.sort();
} get sortedStringArray() {
return this.stringArray.sort();
}
}

结果展示

如果对前端不熟悉的人可能有点疑问。

1. 数字比较没有按照常规的大小顺序?

2. 字符排序没有按照 abcd等顺序?

当我们查看官方的API以后,可以发现:

1. sort比较第一件事先将每一个item转换成字符,也就是说即使整型他也会先转成字符;

2. 按照unicode位点进行排序。我们查看ASC码可以看出来A对应65,B对应66,a对应97,b对应98。 所以排序为 ABab。

显然这种排序不符合我们的要求,如何进行自定义的排序?

sort基于原地算法,方法提供了一个可选择的比较函数,可以提供两个参数,这两个参数相邻的并对这两个参数进行比较。我们只需要进行自定义的sort方法设置,通过比较a,b两个参数是否大于0即可知道a/b两个谁大然后array便会对两个参数进行自定义排序。

在lwc中,可以使用 array.sort((a,b) => {logic})方式去操作,a代表第一个item,b代表第二个item。logic返回大于0或者小于等于0即可。

我们对代码进行简单的变换。

import { LightningElement } from 'lwc';

export default class ArraySortSample extends LightningElement {

    integerArray = [2,5,23,4,16];

    stringArray = ['a','A','b','B'];

    items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 37 },
{ name: 'And', value: 45 },
{ name: 'The', value: -12 },
{ name: 'Magnetic' },
{ name: 'Zeros', value: 37 }
]; get sortedItemArray() {
return this.items.sort((a,b) => {
return (a.value - b.value);
});
} get sortedIntegerArray() {
return this.integerArray.sort((a,b) => a - b);
} get sortedStringArray() {
//return this.stringArray.sort();
return this.stringArray.sort((a,b) => {
a = a.toLocaleLowerCase();
b = b.toLocaleLowerCase();
if(a <= b) {
return -1;
} else {
return 1;
}
});
}
}

对应的arraySortSample.html如下:

<template>
<lightning-card title="integer item sort">
<ul>
<template for:each={sortedIntegerArray} for:item="integerItem">
<li key={integerItem}>
{integerItem}
</li>
</template>
</ul>
</lightning-card>
<lightning-card title="string item sort">
<ul>
<template for:each={sortedStringArray} for:item="stringItem">
<li key={stringItem}>
{stringItem}
</li>
</template>
</ul>
</lightning-card> <lightning-card title="object item sort">
<ul>
<template for:each={sortedItemArray} for:item="objectItem">
<li key={objectItem.value}>
{objectItem.name}
</li>
</template>
</ul>
</lightning-card>
</template>

结果展示

总结:篇中主要简单介绍了一下如何进行自定义sort以及标准sort的排序标准。篇中错误地方欢迎指出,有不懂欢迎留言。

Salesforce LWC学习(二十四) Array.sort 浅谈的更多相关文章

  1. Salesforce LWC学习(二十三) Lightning Message Service 浅谈

    本篇参考: https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist https://d ...

  2. Salesforce LWC学习(二十六) 简单知识总结篇三

    首先本篇感谢长源edward老哥的大力帮助. 背景:我们在前端开发的时候,经常会用到输入框,并且对这个输入框设置 required或者其他的验证,当不满足条件时使用自定义的UI或者使用标准的 inpu ...

  3. Salesforce LWC学习(二十二) 简单知识总结篇二

    本篇参看: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reactivity_fi ...

  4. Salesforce LWC学习(二十五) Jest Test

    本篇参看: https://trailhead.salesforce.com/content/learn/modules/test-lightning-web-components https://j ...

  5. Salesforce LWC学习(三十四) 如何更改标准组件的相关属性信息

    本篇参考: https://www.cnblogs.com/zero-zyq/p/14548676.html https://www.lightningdesignsystem.com/platfor ...

  6. Salesforce LWC学习(二十八) 复制内容到系统剪贴板(clipboard)

    本篇参考: https://developer.mozilla.org/zh-CN/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipb ...

  7. Salesforce LWC学习(二十九) getRecordNotifyChange(LDS拓展增强篇)

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/data_ui_api https ...

  8. Salesforce LWC学习(三十) lwc superbadge项目实现

    本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...

  9. Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...

随机推荐

  1. Linux无名管道通信介绍

    Linux下无名管道一般仅用于父子进程间的通信: 测试代码如下 //file name: fifo_test.c #include <sys/prctl.h> #include " ...

  2. 无法加载文件或程序集“System.Net.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”

    原因是:System.Net.Http.dll 使用了net4.6的版本的.而System.Net.Http.Formatting.dll使用了4.5的版本. 解决方案:将webconfig文件下的n ...

  3. JS 获取验证码按钮改变案例

    HTML代码 <div class="box"> <label for="">手机号</label> <input t ...

  4. vue中methods互相调用的方法

    a:function(goods) { this.aa= []; this.bb= 0; this.cc= 0; }, b:function(){ if(this.bbb!= 0){ this.aa= ...

  5. C#LeetCode刷题之#203-删除链表中的节点(Remove Linked List Elements)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3826 访问. 删除链表中等于给定值 val 的所有节点. 输入: ...

  6. 面试不知如何回答这六大知识点,你还敢说熟悉MySQL?

    文章目录 一.事务 1. 什么是事务 2. 事务的四大特性 3. 事务的并发问题 ① 事务并发问题什么时候发生? ② 事务的并发问题有哪些? ③ 如何避免事务的并发问题? 二.索引 1. 什么是索引 ...

  7. 简单快速搭建钓鱼wifi

    前言 钓鱼wifi是很久的话题了,但是传统的方法可能比较麻烦需要手动配置dhcp,dns,网卡,流量转发,比较麻烦,而且还有根据每次的网络环境需要重新的配置,这里介绍用WIFIpumpkin3工具简单 ...

  8. python的一些使用体会

    python刚开始接触,因为刚好有点需求,所以写了点小程序,一点点体会. 优点: 1. os.rename()方法不错,c#就没有这个方法 2.字符串的slice操作不错,取substring有时比较 ...

  9. hiho一下第零周(题目1 : A + B)

    #include<iostream> using namespace std; int main() { int a,b; while(cin>>a>>b) cou ...

  10. Finding the Right EAV Attribute Table

    $customer = Mage::getModel('catalog/product'); $entity = $customer->getResource(); $attribute = M ...