import UIKit
import Foundation
extension Array {
mutating func order(prep: (T,T) -> Bool )-> (Array) {
//定义一个临时数组
var temp = [T]()
//如果当前数组计数小于2个,就返回自身
if self.count < 2 {
return self
}else {
//循环当前数组
do {
//取出值放入变量
var index = 0
var orderest = self[index]
//取出数组标号放入变量
var indexOrderest = index
//循环当前数组,初始值为第二个数组值
for var inde = index+1 ; inde < self.count; ++inde {
//根据闭包比较的结果存放比较值
orderest = prep(orderest,self[inde]) ? orderest : self[inde]
indexOrderest = prep(orderest,self[inde]) ? indexOrderest : inde
}
//将循环一遍的结果值放入临时数组
temp.append(orderest)
//删除原来数组的对应值
self.removeAtIndex(indexOrderest)
} while self.count > 1
//将最后一个传到临时组
temp.append(self[1])
self = temp
return self
}
}