简体   繁体   English

排序一个NSArray并与第二个数组匹配顺序?

[英]Sort One NSArray and Match Order with Second Array?

I have two NSMutabeArrays, one full of numbers, the other full of words. 我有两个NSMutabeArrays,一个充满数字,另一个充满单词。

Lets say the two arrays look like this: 可以说两个数组如下所示:

1  hello
24 no
11 there
5  boo

Now I order the first array so the numbers are descending (and as below I want the words to move with the numbers so they stay with the original number they are associated with): 现在,我对第一个数组进行排序,使数字递减(如下所示,我希望单词随数字一起移动,以使其与关联的原始数字保持一致):

24 no
11 there
5  boo
1  hello

Any idea how I can achieve this? 知道我该如何实现吗? Thanks. 谢谢。

You should combine the numbers and words into a single object that has properties for both. 您应该将数字和单词组合成一个具有两者属性的对象。 You can then maintain an ordered array of those objects rather than trying to coordinate two independent arrays. 然后,您可以维护这些对象的有序数组,而不必尝试协调两个独立的数组。

One way to achieve this is with a NSDictionary , but I typically recommend a trivial model object to hold the two values: NSDictionary是实现此目的的一种方法,但是我通常建议使用平凡的模型对象来保存两个值:

@interface SomeObject : NSObject
@property (nonatomic, readwrite, strong) NSString *word;
@property (nonatomic, readwrite, assign) NSUInteger number;
@end

@implementation SomeObject
@synthesize word=_word;
@synthesize number=_number;
@end

There are other ways to build this object (you could make it immutable for instance, which is good for multi-threaded apps), but this is the simplest. 还有其他方法可以构建此对象(例如,可以使其不可变,这对于多线程应用程序非常有用),但这是最简单的。

These two arrays are obviously very tightly coupled, so you want to reflect this coupling in the program design, and not just have it buried in the implementation of the sort routine. 这两个数组显然是非常紧密地耦合在一起的,因此您要在程序设计中反映这种耦合,而不仅仅是将其埋在排序例程的实现中。 How best to do this depends on the context you have to fit the solution into. 如何最好地做到这一点取决于您必须适合该解决方案的环境。 The single array solution may be best, or it may not. 单阵列解决方案可能是最好的,也可能不是最好的。 Another possible solution is to create a new class that contains the two arrays and is responsible for maintaining their consistency when sorting, inserting, deleting etc. 另一种可能的解决方案是创建一个包含两个数组的新类,并负责在排序,插入,删除等过程中保持它们的一致性。

While not the most optimal answer, but certainly a way I have implemented multi-column tables (sortable by any column). 虽然这不是最佳的答案,但肯定是我实现了多列表(按任何列排序)的一种方式。

I did mine this way so that the code itself would be more transportable between platforms. 我确实以这种方式进行挖掘,以使代码本身在平台之间具有更大的可移植性。

the way to do this would be to hold an index mapping array. 做到这一点的方法将是持有一个索引映射数组。 The way this works is, you have dataArray1 and dataArray 2. If you want to sort by dataArray1, then you hold the new mapped indeces (as you are sorting the dataArray1) in the mapping array. 它的工作方式是拥有dataArray1和dataArray2。如果要按dataArray1进行排序,则将新的映射索引(在对dataArray1进行排序时)保存在映射数组中。 After you have finished sorting, the data held by the mapping array will be the index from the dataTable2 that needs to be placed in the position of the index of the mapping array. 完成排序后,映射数组所保存的数据将是dataTable2中的索引 ,需要将其放置在映射数组的索引位置

I see a lot of answers that talk about coupling of the data. 我看到很多有关数据耦合的答案。 This heavily depends on how you really intend to use the data. 这在很大程度上取决于您真正打算如何使用数据。 In my design, the data needed coupling only when shown in the table view (and for display of the table), but within my graph view, the data ordering didnt in this manner didnt matter, so no coupling was needed. 在我的设计中,仅当在表视图(以及用于显示表)中显示数据时才需要耦合,但是在我的图形视图中,数据排序并不以这种方式无关紧要,因此不需要耦合。 I think there are many good answers here, the question is what are designing around this data??? 我认为这里有很多很好的答案,问题是围绕这些数据设计的是什么???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM