Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Last active August 29, 2015 13:57
Show Gist options
  • Save anzfactory/9780586 to your computer and use it in GitHub Desktop.
Save anzfactory/9780586 to your computer and use it in GitHub Desktop.
CCDictionaryを保持しているCCArrayやCCDictionaryのソート
void CCArraySort::test()
{
// ソート対象がCCArrayの場合
auto table = makeDummyList();
std::sort(table->data->arr, table->data->arr + table->data->num, compare);
// 確認ログ
CCObject* item;
CCARRAY_FOREACH(table, item) {
auto row = (CCDictionary *)item;
CCLog("name:%s - price:%s",
((CCString *)row->objectForKey("name"))->getCString(),
((CCString *)row->objectForKey("price"))->getCString());
}
// ソート対象がCCDictionaryの場合
auto table2 = makeDummyList2();
// 一旦全部取り出し
CCArray *values = CCArray::create();
CCDictElement *elem;
CCDICT_FOREACH(table2, elem) {
values->addObject(elem->getObject());
}
// ほいでソート
std::sort(values->data->arr, values->data->arr + values->data->num, compare);
// 確認ログ
CCLog("-----------------------");
CCARRAY_FOREACH(values, item) {
auto row = (CCDictionary *)item;
// キーが欲しいなら
auto key = table2->allKeysForObject(row);
// 全く同じデータをもつのが複数いたら判別できなさげ
CCLog("name:%s - price:%s [key:%s]",
((CCString *)row->objectForKey("name"))->getCString(),
((CCString *)row->objectForKey("price"))->getCString(),
((CCString *)key->objectAtIndex(0))->getCString());
}
}
// 比較用メソッド (static定義)
bool CCArraySort::compare(cocos2d::CCObject *a, cocos2d::CCObject *b)
{
CCDictionary *friendA = (CCDictionary*)a;
CCDictionary *friendB = (CCDictionary*)b;
return ((CCString *)friendA->objectForKey("price"))->intValue() > ((CCString *)friendB->objectForKey("price"))->intValue();
// 昇順は比較演算子を変えればok
//return ((CCString *)friendA->objectForKey("price"))->intValue() < ((CCString *)friendB->objectForKey("price"))->intValue();
}
// ダミーデータ作成(CCArray)
CCArray* CCArraySort::makeDummyList()
{
auto arr = CCArray::create();
/* こんなテーブル想定
| name | price |
| ------ | ----- |
| item 0 | 10 |
| item 1 | 50 |
| item 2 | 10 |
| item 3 | 100 |
| item 4 | 30 |
*/
auto prices = CCArray::create(CCString::create("10"),
CCString::create("50"),
CCString::create("10"),
CCString::create("100"),
CCString::create("30"),
NULL);
CCDictionary *item;
for (int i = 0; i < 5; i++) {
item = CCDictionary::create();
item->setObject(CCString::createWithFormat("item %d", i), "name");
item->setObject(prices->objectAtIndex(i), "price");
arr->addObject(item);
}
return arr;
}
// ダミーデータ作成(CCDictionary データ中身は一緒)
CCDictionary* CCArraySort::makeDummyList2()
{
auto dic = CCDictionary::create();
/* こんなテーブル想定
| name | price |
| ------ | ----- |
| item 0 | 10 |
| item 1 | 50 |
| item 2 | 10 |
| item 3 | 100 |
| item 4 | 30 |
*/
auto prices = CCArray::create(CCString::create("10"),
CCString::create("50"),
CCString::create("10"),
CCString::create("100"),
CCString::create("30"),
NULL);
CCDictionary *item;
for (int i = 0; i < 5; i++) {
item = CCDictionary::create();
item->setObject(CCString::createWithFormat("item %d", i), "name");
item->setObject(prices->objectAtIndex(i), "price");
dic->setObject(item, CCString::createWithFormat("key-%d", i)->getCString());
}
return dic;
}
## 出力ログ
Cocos2d: name:item 3 - price:100
Cocos2d: name:item 1 - price:50
Cocos2d: name:item 4 - price:30
Cocos2d: name:item 0 - price:10
Cocos2d: name:item 2 - price:10
Cocos2d: -----------------------
Cocos2d: name:item 3 - price:100 [key:key-3]
Cocos2d: name:item 1 - price:50 [key:key-1]
Cocos2d: name:item 4 - price:30 [key:key-4]
Cocos2d: name:item 0 - price:10 [key:key-0]
Cocos2d: name:item 2 - price:10 [key:key-2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment