Page({
data: {
objectArray: [
{ id: 3, unique: 'unique_5' },
{ id: 2, unique: 'unique_4' },
{ id: 4, unique: 'unique_3' },
{ id: 1, unique: 'unique_2' },
{ id: 5, unique: 'unique_1' },
{ id: 0, unique: 'unique_0' },
],
numberArray: [1, 2, 3, 4]
},
switch: function (e) {
const length = this.data.objectArray.length
for (let i = 0; i < length; ++i) {
// const x = Math.floor(Math.random() * length) //杂序代码
// const y = Math.floor(Math.random() * length)
// const temp = this.data.objectArray[x]
// this.data.objectArray[x] = this.data.objectArray[y]
// this.data.objectArray[y] = temp
for(let j = 0;j < length - 1 - i; j++){ //顺序代码,使用冒泡排序方法,判定数据的大小来排序,但是需要注意,x和y的取值,在这里this指代的是语句,而不是单纯的数据,所以我们要使用其中的ID数据来判断大小,之后冒泡排序就可以实现数据的排序
var x = this.data.objectArray[j].id
var y = this.data.objectArray[j + 1].id
console.log(x)
console.log(y)
if (x > y){
let y = this.data.objectArray[j]
this.data.objectArray[j] = this.data.objectArray[j + 1]
this.data.objectArray[j + 1] = y
} else {
}
}
}
this.setData({
objectArray: this.data.objectArray
})
},
addToFront: function (e) {
const length = this.data.objectArray.length
this.data.objectArray = [{ id: length, unique: 'unique_' + length }].concat(this.data.objectArray)
this.setData({
objectArray: this.data.objectArray
})
},
addNumberToFront: function (e) {
this.data.numberArray = [this.data.numberArray.length + 1].concat(this.data.numberArray)
this.setData({
numberArray: this.data.numberArray
})
}
})
补充上一篇按钮排序
最新推荐文章于 2019-08-10 14:40:00 发布