typescript冒泡排序,类型转换

typescript冒泡排序,类型转换,第1张

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1297281.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存