[Typescript] Sorting arrays in TypeScript
2024-08-31 21:23:10
In this lesson we cover all the details of how to sort a list of items using TypeScript. We also present a few tricks to make your sort logic more readable and maintainable using TypeScript.
.sort() function is a mutation function, it means it will mutate the original array by default.
To prevent mutation:
const arr: ReadonlyArray<string> = ['foo', 'bar'];
const copy = arr.slice().sort();
Here we use 'ReadonlyArray<T>' to tell Typescript, this is a readonly array of string type. So IDE will tell you if you try to mutate the array.
Second, to avoid mutation, we use 'arr.slice()' to copy the original array, then do the sorting.
最新文章
- Sql Server系列:数据库对象
- Retroactive priority queues
- css hover对其包含的元素进行样式设置
- ArcGIS 10.1 中的style和serverstyle及制作方法
- HTML5基础-Mark标签高亮显示文本
- 【心得&;&;体会】
- JS初学之-效果没出来怎么办?-alert函数测试
- 关于sql语句in的使用注意规则
- uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)
- iOS跳转系统设置界面
- Servlet实现文件上传(深度)(二)
- 使用python操作mysql
- Mac安装Elasticsearch时提示:No Java runtime present, requesting install.
- 野路子码农系列(3)plotly可视化的简单套路
- python全栈开发day21-2 几个装饰器总结
- 通过 rufus 创建启动U盘,安装 VMWare Esxi
- KDiff
- iOS 静态库生成(引用第三方SDK、开源库、资源包)
- graphviz 程序生成多种类型图表详解
- VC6IDE环境宏辅助添加移除注释