["Sato", "Aoki", "Ono"] and other array with the name string
"How do you rearrange them in alphabetical order, such as ""Aoki,"" ""Ono,"" and ""Sato""?"
Do you have it?
class person:NSObject {
var name —NSString?
init(nam:NSString){
name=nam
}
}
var person1 = person(nam: "Ono")
var person2 = person(nam: "Sato")
var person3 = person(nam: "Aoki")
println (person1.name)
println (person2.name)
println (person3.name)
Is it possible for an array with such an instance?
swift
You can do it like this.
var strings=["Sato", "Aoki", "Ono", "Kamikido", "Suzuki"]
sort(string) {str1, str2 instr1>str2}
strings.sort {(lhs,rhs) in return lhs<rhs}
http://qiita.com/mst/items/b18e9531ac0cbdf2f3c3
Edit
class person{
var name —NSString
init(nam:NSString){
name=nam
}
}
var person1 = person(nam: "Ono")
var person2 = person(nam: "Sato")
var person3 = person(nam: "Aoki")
var people —Array<person>=[ ]
people+=[person1]
people+=[person2]
people+=[person3]
var sortedPeople=people.sorted({($0.name as?) String) <($1.name as? String)})
println (person1.name)
println (person2.name)
println (person3.name)
println(sortedPeople[0].name)
println(sortedPeople[1].name)
println (sortedPeople[2].name)
If you want to use katakana, you can do it like this.
extension String {
funchiragana()->String{
var str=""
for sin unicodeScalars {
if c.value>=0x30A1&c.value<=0x30F6{
str.append (UnicodeScalar(c.value-96))
} else{
str.append(c)
}
}
return str
}
}
class person {
var name —NSString
init(nam:NSString){
name=nam
}
}
var person1 = person(nam: "Ono")
var person2 = person(nam: "Sato")
var person3 = person(nam: "Aoki")
var person4 = person(nam: "Kamikido")
var people —Array<person>=[ ]
people+=[person1]
people+=[person2]
people+=[person3]
people+=[person4]
var sortedPeople=people.sorted({($0.name as String).hiragana()<($1.name as String).hiragana()})
println(person1.name) // Most
println(person2.name) // _yomi
println(person3.name) // Aoki
println(person4.name)// Kamikido
println (sortedPeople[0].name) // Blue
println(sortedPeople[1].name) // Most
println (sortedPeople[2].name) // Kamikido
println (sortedPeople[3].name) // _yomi
© 2024 OneMinuteCode. All rights reserved.