Sorting NSMutableArray? - iphone

I want to be sort NSMutableArray and its structure is something like that,
Object1:NSMutableDictionary
Affiliation = 2165;
CallLetters = abc;
Channel = "2.1";
ChannelLocation = "";
ChannelSchedules = (
);
DisplayName = abc;
DvbTriplets = (
);
FullName = "bc";
IconAvailable = 1;
Order = 1;
ParentNetworkId = 2;
ServiceType = Digital;
SourceAttributeTypes =
HD
SourceId = 11222;
SourceType = Broadcast;
TiVoSupported = 1;
Type = "24-Hours";
Object2:NSMutableDictionary
Affiliation = 1209;
CallLetters = "xyz";
Chann?el = "4.1";
ChannelLocation = "";
ChannelSchedules = (
);
DisplayName = "xyz";
DvbTriplets = (
);
FullName = "xyz";
IconAvailable = 1;
Order = 2;
ParentNetworkId = 5;
ServiceType = Digital;
SourceAttributeTypes = HD
SourceId = 111
SourceType = Broadcast;
TiVoSupported = 1;
Type = "24-Hours";
VirtualChannelNumber = "4.1";
...
..
.
.
.
./
The array contains many objects, and those objects contain many dictionaries. I want to be able to arrange the above array in ascending order using the NSMutableDictionary key "Channel" ?
How can I sort the array?

Use NSSortDescriptor it will work
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Channel" ascending:YES];
[yourarrayobject sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
[aSortDescriptor release];

To sort your array of objects you:
setup NSSortDescriptor - use names of your variables as keys to
setup descriptor for sorting plus the selector to be executed on
those keys
get the array of descriptors using NSSortDescriptor that you've
setup
sort your array based on those descriptors
How to sort NSMutableArray using sortedArrayUsingDescriptors?

Related

Extract fields from Structure Array to put into another Structure Array

I have a structure array with a large number of fields that I don't care about, so I want to extract the limited number of fields I DO care about and put it into a separate structure array.
For a structure array of size one, I've done this by creating the new array from scratch, for example:
structOld.a = 1;
structOld.b = 2;
structOld.usefulA = 'useful information';
structOld.usefulB = 'more useful information';
structOld.c = 3;
structOld.d = 'words';
keepFields = {'usefulA','usefulB'};
structNew = struct;
for fn = keepFields
structNew.(fn{:}) = structOld.(fn{:});
end
which gives
structNew =
usefulA: 'useful information'
usefulB: 'more useful information'
Is there a more efficient way of doing this? How can I scale up to an structure array (vector) of size N?
N = 50;
structOld(1).a = 1;
structOld(1).b = 2;
structOld(1).usefulA = 500;
structOld(1).usefulB = 'us';
structOld(1).c = 3;
structOld(1).d = 'ef';
structOld(2).a = 4;
structOld(2).b = 5;
structOld(2).usefulA = 501;
structOld(2).usefulB = 'ul';
structOld(2).c = 6;
structOld(2).d = 'in';
structOld(3).a = 7;
structOld(3).b = '8';
structOld(3).usefulA = 504;
structOld(3).usefulB = 'fo';
structOld(3).c = 9;
structOld(3).d = 'rm';
structOld(N).a = 10;
structOld(N).b = 11;
structOld(N).usefulA = 506;
structOld(N).usefulB = 'at';
structOld(N).c = 12;
structOld(N).d = 'ion';
In this case, I'd like to end up with:
structNew =
1x50 struct array with fields:
usefulA
usefulB
Keeping elements with empty usefulA/usefulB fields is fine; I can get rid of them later if needed.
Using rmfield isn't great because the number of useless fields far outnumbers the useful fields.
You can create a new struct array using existing data as follows:
structNew = struct('usefulA',{structOld.usefulA},'usefulB',{structOld.usefulB});
If you have an arbitrary set of field names that you want to preserve, you could use a loop as follows. Here, I'm first extracting the data from strcutOld into a cell array data, which contains each of the arguments the the struct call in the previous line of code. data{:} is now a comma-separated list of these arguments, the last line of code below is identical to the line above.
keepFields = {'usefulA','usefulB'};
data = cell(2,numel(keepFields));
for ii=1:numel(keepFields)
data{1,ii} = keepFields{ii};
data{2,ii} = {structOld.(keepFields{ii})};
end
structNew = struct(data{:});

PowerShell accessing data from hashtable inside hashtable

I have 5 hash tables:
$Monday = #{RUG = "";NRH1 = "";NRH2 = "";ELM = "";BAGVAGT = ""}
$Tuesday = #{RUG = "";NRH1 = "";NRH2 = "";ELM = "";BAGVAGT = ""}
$Wednesday = #{NRH1 = "";NRH2 = "";ELM = "";BAGVAGT = ""}
$Thursday = #{NRH1 = "";NRH2 = "";ELM = "";BAGVAGT = ""}
$Friday = #{NRH1 = "";NRH2 = "";ELM = ""}
That get filled with data. And I can get data out from these either one at at time with $Monday.RUG or all with $Monday | out-string. No problem there.
I'm going to combine those in another hash table 100 times with different data.
So it will be like this:
$Week = #{
1 = #{mo=$Monday;tu=$Tuesday;we=$Wednesday;th=$Thursday;fr=$Friday;val=$value}
2 = #{mo=$Monday;tu=$Tuesday;we=$Wednesday;th=$Thursday;fr=$Friday;val=$value}
}
And so on, until I have 100 different weeks with different values (value will be a calculated number)
But the question is. How do I access the items in the hashtables inside the $week hashtable?
Is there a direct way like $week.1.mo ? or do you need to use a loop?
You can access it using:
$Week[1].mo

handle JSON response in project

I am a new programmer. I get the following response from server. How can i get the value of 0 index "Mile High Motors of Butte" and "Mile High Motors of Dillion" from following
Thanks
{
dealer = (
{
0 = "Mile High Motors of Butte";
1 = "3883 Harrison";
2 = Butte;
3 = 59701;
4 = MT;
5 = "http://www.buttesmilehighchryslerjeepdodge.com";
6 = 2;
7 = 0;
address = "3883 Harrison";
city = Butte;
distance = 0;
id = 2;
name = "Mile High Motors of Butte";
state = MT;
url = "http://www.buttesmilehighchryslerjeepdodge.com";
zip = 59701;
},
{
0 = "Mile High Motors of Dillon";
1 = "790 N Montana St";
2 = Dillon;
3 = 59725;
4 = Montana;
5 = "http://www.MileHighDillon.com";
6 = 13;
7 = "60.1235269593172";
address = "790 N Montana St";
city = Dillon;
distance = "60.1235269593172";
id = 13;
name = "Mile High Motors of Dillon";
state = Montana;
url = "http://www.MileHighDillon.com";
zip = 59725;
}
);
success = 1;
}
Okay let's see your structure (assuming that you have already deserialized your JSON string).
You have an NSDictionary with two keys (dealer & success). Now dealer key is an NSArray with two NSDictionaries. So based on that we could do:
NSDictionary *myJson; // Assuming that this is what you have posted
NSArray *dealers = [myJson valueForKey:#"dealer"];
// Now just grab whatever you need
NSString *dealerOne = [[dealers objectAtIndex:0] valueForKey:#"0"]; //Mile High Motors of Butte
NSString *dealerTwo = [[dealers objectAtIndex:1] valueForKey:#"0"]; //Mile High Motors of Dillon
Or you could just iterate your dealers array like this:
for (NSDictionary *dealer in dealers)
{
NSString *dealerName = [dealer valueForKey:#"0"];
// Do something useful
}
NSMutableArray yourStringArray= [[NSMutableArray alloc] init]; //for getting texts what you want.
NSArray * array1 = [yourDictionary valueForKey:#"dealer"];// You will get array which has dictionary elements.
for (NSDictionary *dealer in array1)// Write loop for getting your string.
{
NSString *dealerName = [dealer valueForKey:#"0"];
[yourStringArray addObject:dealerName];
}
I think it will be helpful to you.

How retrireve array from dictionary

I have 2 columns in my Sqlite table 1.DetailsID & 2.Detailstype
i have stored values id: int and detailstype :varchar.
set the id with string in sqlite select query as
while(sqlite3_step(selectPrefer) == SQLITE_ROW)
{
NSString *detailsString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectPrefer, 1)];
int detailsId = (int)sqlite3_column_int(selectPrefer, 0);
[detailsData setObject:detailsString forKey:[NSNumber numberWithInt:detailsId ]];
}
I have a NSmutable dictionary like this:
(
0 = "˚F";
12 = Activity;
11 = BM;
7 = "Heart Rate";
6 = "Nose Problem";
2 = Rx;
1 = BP;
10 = Food;
9 = "Stress Level";
8 = Glucose;
5 = "Pain Level";
4 = Weight;
3 = Events;
}
i can get arrays using allKeys & allValues but these are not in order
Now i want seperate arrays like
{
0
1
2
3
4
5
6
7
8
9
10
11
12
)
both values & keys In ascending order with respect to keys
(
"˚F";
Activity;
BM;
"Heart Rate";
"Nose Problem";
Rx;
BP;
Food;
"Stress Level";
Glucose;
"Pain Level";
Weight;
Events;
)
with out any modifications in sqlite query
- what to do thannks in advance
You should use the allKeys and allValues property of the dictionay :
allKeys Returns a new array containing the dictionary’s keys.
allValues Returns a new array containing the dictionary’s values.
Try this :
NSArray *keysArray = [yourDictionnay allKeys];
NSArray *valuesArray = [yourDictionnay allvalues];
Hope this helps,
Vincent
you need both allValues and allkeys.
NSArray *values = [dictionary allValues];
NSArray *keys = [dicitoary allKeys];

Array index is always 0, can't get it to iterate through the array in a forloop. Using in MapView

I have an array that I want to show on a mapvew, the forloop iterates fine but the index of thew array is always 0.
self.clientTable = [ClientDatabase database].clientTable;
ClientTable *info = nil;
[_nameLabel setText:info.name];
[_stateLabel setText:info.state];
int countArray = [self.clientTable count];
for (int i=0;i<countArray;i++) {
info.uniqueId=i;
NSLog(#" i = %d ; id = %d",i, info.uniqueId);
}
however the results are always
24
i = 0 ; id = 0
i = 1 ; id = 0
i = 2 ; id = 0
i = 3 ; id = 0
i = 4 ; id = 0
i = 5 ; id = 0
I know the array has data as it displays in the tableview fine.
Any ideas?
The reason for the above is displaying each item in a mapview.
Thankyou!
Before this line
info.uniqueId=i;
are you missing something like
info = [self.clientTable objectAtIndex:i]
?? In the code you've provided you set info to nil, but never to anything else.
Because you set info to nil, right up there.