0
点赞
收藏
分享

微信扫一扫

typescript冒泡排序,类型转换

zhyuzh3d 2022-05-18 阅读 62
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

举报

相关推荐

0 条评论