이것은 현재 모델 클래스 코드입니다.Swift에서 이름을 알파벳순으로 정렬하려면 어떻게합니까?
final class ContactData
{
static let sharedInstance = ContactData()
private var contactList : [Contact] =
[Contact(name:"Mike Smith",email:"[email protected]"),
Contact(name:"John Doe",email:"[email protected]"),
Contact(name:"Jane Doe",email:"[email protected]")]
private init()
{
// SORTING HERE
}
var index = 0
func newContact(new:Contact)
{
contactList.append(new)
//sort
}
func updateContact(updated:Contact)
{
contactList[index]=updated
//sort
}
func previousContact() -> Contact
{
index-=1
if index < 0
{
index = contactList.count-1
}
return contactList[index]
}
func nextContact() -> Contact
{
index+=1
if index == contactList.count
{
index = 0
}
return contactList[index]
}
func firstContact() -> Contact
{
return contactList[0]
}
func currentContact() -> Contact
{
return contactList[index]
}
}
모든 것이 제대로 실행되고 있지만 사용 Contact
의 배열을 알파벳순으로 시도했다 :
var sortedContacts = contactList.sorted{
$0.localizedCaseinsensitiveCompare($1)==ComparisonResult.orderedAscending}
}
을하지만 오류가 점점 오전 :
Value of type '
Contact
' has no member 'localizedCaseinsensitiveCompare
'
여기에 질문을 통해 상대를 배열을 영문자 순으로 알 수있는 유일한 방법을 찾을 수있었습니다.
나는 당신이 소문자를 구별 Contact
사용자 정의 클래스의 경우를 비교할 수 없습니다 스위프트 3, 엑스 코드 8.3
이 솔루션을 고려해야 할 몇 가지 사항이 있습니다. 모든 것은'name'에만 묶여 있습니다. 이것은 당신이 항상 이름으로 비교하고 이메일을 무시하기를 원한다고 가정합니다. 그것은 동일한 이름이지만 이메일이 다른 두 개의 다른 연락처를 동일하게 취급합니다. 그것은 미묘한 버그를 많이 일으킬 수 있습니다. 전자 우편이 비교되는 경우 이름이 동등한 것이 가장 좋습니다. – rmaddy
내 의견은 OP에 자신의 맞춤 정렬을 제공합니다 –
또는 설명 비교 –