Is there anyway to shuffle Special Sets of Item in NSMutableArray?
for example this is my NSMutableArray :
self.resultsdict : (
{
Id = 261496;
packID = 1057;
title = "cat"
},
{
Id = 261497;
packID = 1057;
title = "dog"
},
{
Id = 261498;
packID = 1057;
title = "fish"
},
{
Id = 261499;
packID = 1059;
title = "donkey"
},
{
Id = 261500;
packID = 1060;
title = "cow"
},
now i want to shuffle items with the "packID = 1057" in the list. there other item should be with
normal order.
output for title should be : [ cat , dog , fish ] <- in random order and the rest [ donkey , cow]
in normal order.
i can shuffle all list with this code , but is there any way to modify it for above propose ?
for (int x = 0; x < [self.resultsdict count]; x++) {
int randInt = (arc4random() % ([self.resultsdict count] - x)) + x;
[self.resultsdict exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
one solution that i find out is to make two NSMutableArray , one for shuffle and another one for
normal order and after shuffle i them merge with addObjectsFromArray.
but this solution is kinda hard for multiple shuffle list , like this :
self.resultsdict : (
{
Id = 261496;
packID = 1057;
title = "cat"
},
{
Id = 261497;
packID = 1057;
title = "dog"
},
{
Id = 261498;
packID = 1057;
title = "fish"
},
{
Id = 261499;
packID = 1059;
title = "donkey"
},
{
Id = 261500;
packID = 1060;
title = "cow"
},
{
Id = 261501;
packID = 1060;
title = "AyeAye"
},
with following condition => "packID = 1057" and "packID = 1060"
and following output :
[ cat , dog , fish ] <- in random order + [ donkey ] + [ cow , aye aye ] <- in random order.
More info :
the program makes query for test with deferent PackID.
SELECT * FROM tests WHERE packID IN ("1057","1059","1060")
the result of query will be inserted into "self.resultsdict"
FMResultSet *results_packs = [db executeQuery:sql];
while([results_packs next])
{
[self.resultsdict addObject:[results_packs resultDictionary]];
}
user have a shuffle ability for test query. for example he/she can select to show shuffle version
of "1057","1060" and orderd version of "1059".
now what i want to do is modify my normal shuffle code to accept array of condition, like shuffle
item per following keys ("1057","1060").
thanks in forward