zoukankan      html  css  js  c++  java
  • [Typescript] Sorting arrays in TypeScript

    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.

  • 相关阅读:
    Python 基础(二)
    Python 入门
    DNS
    PXE自动化安装CentOS7和CentOS6
    AIDE及sudo应用
    SSH应用
    KickStart自动化安装Linux
    初见鸟哥
    数组ARRAY
    SSH用法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6643160.html
Copyright © 2011-2022 走看看