我是靠谱客的博主 怕孤独香水,这篇文章主要介绍typescript冒泡排序,类型转换,现在分享给大家,希望可以做个参考。

import {Component, OnInit} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'untitled-intent';
  price: string[] = ["11", "23", "45", "22","1","45","25","68","19"];
  priceInt: number[] = [];

  constructor() {
  }

  ngOnInit(): void {
    console.log(`数据结果:${this.priceInt}`)
    for (let i in this.price) {
      this.priceInt[i] = Number(this.price[i]);
    }
    console.log(`数据结果two:${this.priceInt}`)
    for (let i = 0; i < this.priceInt.length - 1; i++) {
      for (let j = 0; j < this.priceInt.length - i - 1; j++) {
        if (this.priceInt[j] > this.priceInt[j + 1]) {
          let temp = this.priceInt[j];
          this.priceInt[j] = this.priceInt[j + 1];
          this.priceInt[j + 1] = temp;
        }
      }
    }
    console.log(`数据结果计划外three:${this.priceInt}`)
  }
}

运行结果:
数据结果:
app.component.ts:21 数据结果two:11,23,45,22,1,45,25,68,19
app.component.ts:31 数据结果计划外three:1,11,19,22,23,25,45,45,68

最后

以上就是怕孤独香水最近收集整理的关于typescript冒泡排序,类型转换的全部内容,更多相关typescript冒泡排序内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部