写一段起泡排序的vue程序代码
时间: 2023-02-20 20:27:21 浏览: 88
关于冒泡排序的代码实现
<template>
<div>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
items: []
}
},
mounted() {
this.items = this.bubbleSort(this.items);
},
methods: {
bubbleSort(items) {
let length = items.length;
for (let i = 0; i < length; i++) {
for (let j = 0; j < length - 1 - i; j++) {
if (items[j].name > items[j + 1].name) {
let temp = items[j + 1];
items[j + 1] = items[j];
items[j] = temp;
}
}
}
return items;
}
}
}
</script>
阅读全文