1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| import UIKit
let testValue1:String = "24,6723,23,111,43,1,昇順" let testValue2:String = "え,い,う,か,と,昇順" let testValue3:String = "-2,-22347999,あ,2,い,3,-1,ああ,昇順"
enum OrderType:String{ case none = "例外" case ascending = "昇順" case descending = "降順" }
enum ValueType:String{ case Iregular = "例外" case Number = "数字" case Charactor = "ローマ字" case Japanese = "日本語" }
var inputValueList = testValue3.split(separator: ",")
var orderType:OrderType = OrderType(rawValue: String(inputValueList.last ?? "")) ?? OrderType.none
if(orderType == OrderType.none){ print("ソート方法を指定してください") print("指定がないため、昇順ソートを行います") orderType = OrderType.ascending }else{ inputValueList.removeLast() }
let valueType = CheckValueType(targetValueList: inputValueList)
var sortedNumberList:Array<Substring> = Array<Substring>() sortedNumberList = Sort(targetNumberList:inputValueList)
switch valueType { case .Number: print(ValueType.Number.rawValue + "ソート完了") case .Charactor: print(ValueType.Charactor.rawValue + "ソート完了") case .Japanese: print(ValueType.Japanese.rawValue + "ソート完了") case .Iregular: print(ValueType.Iregular.rawValue + "ソート完了") }
switch orderType { case .ascending: print(sortedNumberList) case .descending: print(Array(sortedNumberList.reversed())) case .none: print("ソート失敗") }
func CheckValueType(targetValueList:Array<Substring>)-> ValueType{ var retVal : ValueType = ValueType.Iregular let targetValueCount = targetValueList.count var numberCount = 0 var charCount = 0 var japaneseCount = 0 for i in 0...inputValueList.count - 1{ let str:Int = Int(inputValueList[i].cString(using: .shiftJIS)!.first!) if(str >= 48 && str <= 57){ numberCount += 1 }else if(str >= 97 && str <= 122){ charCount += 1 }else if(str >= 65 && str <= 90){ charCount += 1 }else if(str == -125){ japaneseCount += 1 }else if(str == -126){ japaneseCount += 1 } } if(numberCount == targetValueCount){ retVal = ValueType.Number }else if(charCount == targetValueCount){ retVal = ValueType.Charactor }else if(japaneseCount == targetValueCount){ retVal = ValueType.Japanese }else{ retVal = ValueType.Iregular } return retVal }
func Sort(targetNumberList:Array<Substring>)->Array<Substring>{ var sortList = targetNumberList var isChange = false for index in 0...sortList.count - 1{ if(index + 1 == sortList.count) { break } let firstList = sortList[index].cString(using: .shiftJIS)! var firstSign = 1 var firstJapaneseSign = 0 let firstCheckValue = firstList.first! if(firstCheckValue == 45 ) { firstSign = -1 } if(firstCheckValue == -125 || firstCheckValue == -126) { firstJapaneseSign = Int(firstCheckValue) * -1 } var first:Int = 0 for fc in firstList{ first += Int(fc) * firstSign } if(firstJapaneseSign != 0){ first *= firstJapaneseSign } let secondList = sortList[index+1].cString(using: .shiftJIS)! var secondSign = 1 var secondJapaneseCheckSign = 0 let secondCheckValue = secondList.first! if(secondCheckValue == 45) { secondSign = -1 } if(secondCheckValue == -125 || secondCheckValue == -126) { secondJapaneseCheckSign = Int(secondCheckValue) * -1 } var second:Int = 0 for sc in secondList{ second += Int(sc) * secondSign } if(secondJapaneseCheckSign != 0){ second *= secondJapaneseCheckSign }
let firstValue = sortList[index] let secondValue = sortList[index+1] if(first > second) { isChange = true
sortList[index] = secondValue sortList[index + 1] = firstValue } } if(isChange){ sortList = Sort(targetNumberList: sortList) } return sortList }
|