如何按字母顺序对二维数组中的数据进行排序? [英] How to sort the data in in a twoDimensional array alphabetically?

查看:209
本文介绍了如何按字母顺序对二维数组中的数据进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在2D数组中按字母顺序排序数据存在很大问题。我将尽力为您提供尽可能清晰的细节。

I am currently having a big issue sorting my Data alphabetically in a 2D array. I'm going to try to give you every detail to be as clear as possible.

目前,我正在通过CNContactStore获取我的联系人。一切正常。我能够从联系人中检索出我想要的所有数据。

Currently, I am fetching my contacts with the CNContactStore. This all works fine. I am able to retrieve all the data I want out of my contacts.

现在,我创建了以下结构:

Now, I created the following struct:

struct FavoritableContact {
    let contact: CNContact
    var hasFavorited: Bool
}

有了这个,我声明并初始化了以下数组:

With this, I declared and initialized the following array:

var favoritableContacts = [FavoritableContact]()

一旦我检索到我的联系人,我只需将它们附加到favoritableContacts;

Once I retrieved my contacts, I simply appended them to favoritableContacts;

try store.enumerateContacts(with: request, usingBlock: { (contact, stopPointerIfYouWantToStopEnumerating) in

  favoritableContacts.append(FavoritableContact(contact: contact, hasFavorited: false))

})

要在同一个数组中按字母顺序对它们进行排序,我只是做了以下事情:

To sort them in alphabetical order in the same array, I simply did the following:

 var sortedContacts = favoritableContacts.sorted { $0.contact.familyName < $1.contact.familyName }

如果可能,我想创建以下2D数组,

Now if possible, I want to create the following 2D array,

var 2D = [
        [FavoritableContact] //"A"
        [FavoritableContact], //"B"
        [FavoritableContact], //"C"
        [FavoritableContact], //"D"
        ...
    ]

我只是不确定如何取出我的sortedContacts数组并按字母顺序分开。

I am just not sure how to take my sortedContacts array and separate alphabetically.

我在这里很新,如果我忘记了什么,或者我没有做正确的事,请告诉我。

I am very new here, If I forgot something, or I didn't do somethign right please let me know.

推荐答案

正如评论中指出的那样,首字母作为键的字典可能是更好的方法,因为它是更容易访问,但也许你有理由想要使用二维数组。为此,您可以执行以下操作:

As was pointed out in the comments, a dictionary with first letters as keys is probably the better way to go as it is much easier to access, though perhaps you have a reason for wanting to use a 2d array instead. To achieve that you could do something like this:

//Create an empty array filled with 26 arrays of FavorableContact
var array2d = Array<[FavoritableContact]>(repeating: [FavoritableContact](), count: 26)

//Find the ascii value for "A" to use as your base
let aAscii = Int("A".unicodeScalars.filter({ $0.isASCII }).map({ $0.value })[0])  //This returns 65, btw, so you could also just hardcode

//Go through your original array, find the first letter of each contact, and append to the correct array
favoritableContacts.forEach { (contact) in

    //Get the ascii value for the first letter
    let firstLetter = Int(contact.contact.familyName.prefix(1).uppercased().unicodeScalars.filter({ $0.isASCII }).map({ $0.value })[0])

    //Append to the array for this letter by subtracting the ascii value for "A" from the ascii value for the uppercased version of this letter.
    array2d[firstLetter - aAscii].append(contact)
}

this这不是世界上最干净的东西,它采用标准的英语字母表,没有变音符号,符号,数字或其他任何东西。假设这是真的,它就完成了工作。

This is not the cleanest thing in the world, and it assumes standard English language alphabet with no diacritics, symbols, numbers or anything else. Assuming that is true it gets the job done.

这篇关于如何按字母顺序对二维数组中的数据进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆