Add group sorting feature

* Enabling sorting of groups and their children in ascending and descending direction
This commit is contained in:
Balazs Gyurak 2019-06-18 21:58:47 +01:00 committed by Jonathan White
parent 0c2d1bcc50
commit 09181fab13
15 changed files with 373 additions and 0 deletions

View file

@ -840,3 +840,183 @@ void TestGroup::testEquals()
QVERIFY(group->equals(group.data(), CompareItemDefault));
}
void TestGroup::sortChildrenRecursively()
{
auto createTestGroupWithUnorderedChildren = []() -> Group* {
Group* parent = new Group();
Group* group1 = new Group();
group1->setName("B");
group1->setParent(parent);
Group* group2 = new Group();
group2->setName("e");
group2->setParent(parent);
Group* group3 = new Group();
group3->setName("Test999");
group3->setParent(parent);
Group* group4 = new Group();
group4->setName("A");
group4->setParent(parent);
Group* group5 = new Group();
group5->setName("z");
group5->setParent(parent);
Group* group6 = new Group();
group6->setName("045");
group6->setParent(parent);
Group* group7 = new Group();
group7->setName("60");
group7->setParent(parent);
Group* group8 = new Group();
group8->setName("04test");
group8->setParent(parent);
Group* group9 = new Group();
group9->setName("Test12");
group9->setParent(parent);
Group* group10 = new Group();
group10->setName("i");
group10->setParent(parent);
Group* subGroup1 = new Group();
subGroup1->setName("sub_xte");
subGroup1->setParent(group10);
Group* subGroup2 = new Group();
subGroup2->setName("sub_010");
subGroup2->setParent(group10);
Group* subGroup3 = new Group();
subGroup3->setName("sub_000");
subGroup3->setParent(group10);
Group* subGroup4 = new Group();
subGroup4->setName("sub_M");
subGroup4->setParent(group10);
Group* subGroup5 = new Group();
subGroup5->setName("sub_p");
subGroup5->setParent(group10);
Group* subGroup6 = new Group();
subGroup6->setName("sub_45p");
subGroup6->setParent(group10);
Group* subGroup7 = new Group();
subGroup7->setName("sub_6p");
subGroup7->setParent(group10);
Group* subGroup8 = new Group();
subGroup8->setName("sub_tt");
subGroup8->setParent(group10);
Group* subGroup9 = new Group();
subGroup9->setName("sub_t0");
subGroup9->setParent(group10);
return parent;
};
Group* parent = createTestGroupWithUnorderedChildren();
Group* subParent = parent->children().last();
parent->sortChildrenRecursively();
QList<Group*> children = parent->children();
QCOMPARE(children.size(), 10);
QCOMPARE(children[0]->name(), QString("045"));
QCOMPARE(children[1]->name(), QString("04test"));
QCOMPARE(children[2]->name(), QString("60"));
QCOMPARE(children[3]->name(), QString("A"));
QCOMPARE(children[4]->name(), QString("B"));
QCOMPARE(children[5]->name(), QString("e"));
QCOMPARE(children[6]->name(), QString("i"));
QCOMPARE(children[7]->name(), QString("Test12"));
QCOMPARE(children[8]->name(), QString("Test999"));
QCOMPARE(children[9]->name(), QString("z"));
children = subParent->children();
QCOMPARE(children.size(), 9);
QCOMPARE(children[0]->name(), QString("sub_000"));
QCOMPARE(children[1]->name(), QString("sub_010"));
QCOMPARE(children[2]->name(), QString("sub_45p"));
QCOMPARE(children[3]->name(), QString("sub_6p"));
QCOMPARE(children[4]->name(), QString("sub_M"));
QCOMPARE(children[5]->name(), QString("sub_p"));
QCOMPARE(children[6]->name(), QString("sub_t0"));
QCOMPARE(children[7]->name(), QString("sub_tt"));
QCOMPARE(children[8]->name(), QString("sub_xte"));
delete parent;
parent = createTestGroupWithUnorderedChildren();
subParent = parent->children().last();
parent->sortChildrenRecursively(true);
children = parent->children();
QCOMPARE(children.size(), 10);
QCOMPARE(children[0]->name(), QString("z"));
QCOMPARE(children[1]->name(), QString("Test999"));
QCOMPARE(children[2]->name(), QString("Test12"));
QCOMPARE(children[3]->name(), QString("i"));
QCOMPARE(children[4]->name(), QString("e"));
QCOMPARE(children[5]->name(), QString("B"));
QCOMPARE(children[6]->name(), QString("A"));
QCOMPARE(children[7]->name(), QString("60"));
QCOMPARE(children[8]->name(), QString("04test"));
QCOMPARE(children[9]->name(), QString("045"));
children = subParent->children();
QCOMPARE(children.size(), 9);
QCOMPARE(children[0]->name(), QString("sub_xte"));
QCOMPARE(children[1]->name(), QString("sub_tt"));
QCOMPARE(children[2]->name(), QString("sub_t0"));
QCOMPARE(children[3]->name(), QString("sub_p"));
QCOMPARE(children[4]->name(), QString("sub_M"));
QCOMPARE(children[5]->name(), QString("sub_6p"));
QCOMPARE(children[6]->name(), QString("sub_45p"));
QCOMPARE(children[7]->name(), QString("sub_010"));
QCOMPARE(children[8]->name(), QString("sub_000"));
delete parent;
parent = createTestGroupWithUnorderedChildren();
subParent = parent->children().last();
subParent->sortChildrenRecursively();
children = parent->children();
QCOMPARE(children.size(), 10);
QCOMPARE(children[0]->name(), QString("B"));
QCOMPARE(children[1]->name(), QString("e"));
QCOMPARE(children[2]->name(), QString("Test999"));
QCOMPARE(children[3]->name(), QString("A"));
QCOMPARE(children[4]->name(), QString("z"));
QCOMPARE(children[5]->name(), QString("045"));
QCOMPARE(children[6]->name(), QString("60"));
QCOMPARE(children[7]->name(), QString("04test"));
QCOMPARE(children[8]->name(), QString("Test12"));
QCOMPARE(children[9]->name(), QString("i"));
children = subParent->children();
QCOMPARE(children.size(), 9);
QCOMPARE(children[0]->name(), QString("sub_000"));
QCOMPARE(children[1]->name(), QString("sub_010"));
QCOMPARE(children[2]->name(), QString("sub_45p"));
QCOMPARE(children[3]->name(), QString("sub_6p"));
QCOMPARE(children[4]->name(), QString("sub_M"));
QCOMPARE(children[5]->name(), QString("sub_p"));
QCOMPARE(children[6]->name(), QString("sub_t0"));
QCOMPARE(children[7]->name(), QString("sub_tt"));
QCOMPARE(children[8]->name(), QString("sub_xte"));
delete parent;
parent = createTestGroupWithUnorderedChildren();
subParent = parent->children().last();
subParent->sortChildrenRecursively(true);
children = parent->children();
QCOMPARE(children.size(), 10);
QCOMPARE(children[0]->name(), QString("B"));
QCOMPARE(children[1]->name(), QString("e"));
QCOMPARE(children[2]->name(), QString("Test999"));
QCOMPARE(children[3]->name(), QString("A"));
QCOMPARE(children[4]->name(), QString("z"));
QCOMPARE(children[5]->name(), QString("045"));
QCOMPARE(children[6]->name(), QString("60"));
QCOMPARE(children[7]->name(), QString("04test"));
QCOMPARE(children[8]->name(), QString("Test12"));
QCOMPARE(children[9]->name(), QString("i"));
children = subParent->children();
QCOMPARE(children.size(), 9);
QCOMPARE(children[0]->name(), QString("sub_xte"));
QCOMPARE(children[1]->name(), QString("sub_tt"));
QCOMPARE(children[2]->name(), QString("sub_t0"));
QCOMPARE(children[3]->name(), QString("sub_p"));
QCOMPARE(children[4]->name(), QString("sub_M"));
QCOMPARE(children[5]->name(), QString("sub_6p"));
QCOMPARE(children[6]->name(), QString("sub_45p"));
QCOMPARE(children[7]->name(), QString("sub_010"));
QCOMPARE(children[8]->name(), QString("sub_000"));
delete parent;
}