Skip to content

Commit 1f53b3d

Browse files
committed
do not sort lists alphabetically
Now nested lists reflect the result set order again. The only exception being the index level which is always sorted alphabetically. N/A entries are still always last.
1 parent 1855a1f commit 1f53b3d

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

_test/NestedResultTest.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function testSimpleZeroLevel()
107107
$nestedResult = new NestedResult($result);
108108
$root = $nestedResult->getRoot(0);
109109

110-
$this->assertCount(0, $root->getChildren(), 'no children expected');
110+
$this->assertCount(0, $root->getChildren(true), 'no children expected');
111111
$this->assertCount(12, $root->getResultRows(), '12 result rows expected');
112112
}
113113

@@ -120,14 +120,14 @@ public function testSimpleOneLevel()
120120
$result = $this->makeResult($this->simpleItems);
121121
$nestedResult = new NestedResult($result);
122122
$root = $nestedResult->getRoot(1);
123-
$tree = $root->getChildren();
123+
$tree = $root->getChildren(true);
124124

125125
$this->assertCount(2, $tree, '2 root nodes expected');
126126
$this->assertEquals('car', $tree[0]->getValueObject()->getValue());
127127
$this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
128128

129-
$this->assertCount(0, $tree[0]->getChildren(), 'no children expected');
130-
$this->assertCount(0, $tree[1]->getChildren(), 'no children expected');
129+
$this->assertCount(0, $tree[0]->getChildren(true), 'no children expected');
130+
$this->assertCount(0, $tree[1]->getChildren(true), 'no children expected');
131131

132132
$this->assertCount(6, $tree[0]->getResultRows(), 'result rows');
133133
$this->assertCount(6, $tree[1]->getResultRows(), 'result rows');
@@ -145,23 +145,23 @@ public function testSimpleTwoLevels()
145145
$result = $this->makeResult($this->simpleItems);
146146
$nestedResult = new NestedResult($result);
147147
$root = $nestedResult->getRoot(2);
148-
$tree = $root->getChildren();
148+
$tree = $root->getChildren(true);
149149

150150
$this->assertCount(2, $tree, '2 root nodes expected');
151151
$this->assertEquals('car', $tree[0]->getValueObject()->getValue());
152152
$this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
153153

154-
$this->assertCount(2, $tree[0]->getChildren(), '2 second level nodes expected');
155-
$this->assertCount(2, $tree[1]->getChildren(), '2 second level nodes expected');
154+
$this->assertCount(2, $tree[0]->getChildren(true), '2 second level nodes expected');
155+
$this->assertCount(2, $tree[1]->getChildren(true), '2 second level nodes expected');
156156

157-
$this->assertCount(3, $tree[0]->getChildren()[0]->getResultRows(), 'result rows');
158-
$this->assertCount(3, $tree[0]->getChildren()[1]->getResultRows(), 'result rows');
159-
$this->assertCount(3, $tree[1]->getChildren()[0]->getResultRows(), 'result rows');
160-
$this->assertCount(3, $tree[1]->getChildren()[1]->getResultRows(), 'result rows');
157+
$this->assertCount(3, $tree[0]->getChildren(true)[0]->getResultRows(), 'result rows');
158+
$this->assertCount(3, $tree[0]->getChildren(true)[1]->getResultRows(), 'result rows');
159+
$this->assertCount(3, $tree[1]->getChildren(true)[0]->getResultRows(), 'result rows');
160+
$this->assertCount(3, $tree[1]->getChildren(true)[1]->getResultRows(), 'result rows');
161161

162162

163-
$this->assertEquals('a80', $tree[0]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected');
164-
$this->assertEquals('pro 16', $tree[1]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected');
163+
$this->assertEquals('a80', $tree[0]->getChildren(true)[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected');
164+
$this->assertEquals('pro 16', $tree[1]->getChildren(true)[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected');
165165
}
166166

167167
/**
@@ -172,7 +172,7 @@ public function testMultiThreeLevels()
172172
$result = $this->makeResult($this->multiItems);
173173
$nestedResult = new NestedResult($result);
174174
$root = $nestedResult->getRoot(3);
175-
$tree = $root->getChildren(); // nest: color, type, brand -> model
175+
$tree = $root->getChildren(true); // nest: color, type, brand -> model
176176

177177
$this->assertCount(6, $tree, '6 root nodes of colors expected');
178178

@@ -187,16 +187,16 @@ public function testMultiThreeLevels()
187187
// Results should now show up under multiple top-level nodes
188188
$this->assertEquals('a80',
189189
$tree[3] // green
190-
->getChildren()[0] // car
191-
->getChildren()[0] // audi
190+
->getChildren(true)[0] // car
191+
->getChildren(true)[0] // audi
192192
->getResultRows()[0][0] // a80
193193
->getValue(),
194194
'green car audi a80 expected'
195195
);
196196
$this->assertEquals('a80',
197197
$tree[5] // yellow
198-
->getChildren()[0] // car
199-
->getChildren()[0] // audi
198+
->getChildren(true)[0] // car
199+
->getChildren(true)[0] // audi
200200
->getResultRows()[0][0] // a80
201201
->getValue(),
202202
'yellow car audi a80 expected'
@@ -208,10 +208,10 @@ public function testMultiHoles()
208208
$result = $this->makeResult($this->multiHoleItems);
209209
$nestedResult = new NestedResult($result);
210210
$root = $nestedResult->getRoot(3);
211-
$tree = $root->getChildren(); // nest: color, type, brand -> model
211+
$tree = $root->getChildren(true); // nest: color, type, brand -> model
212212
$this->assertCount(7, $tree, '6 root nodes of colors + 1 n/a expected'); // should have one n/a node
213-
$this->assertCount(2, $tree[6]->getChildren(), 'top n/a node should have car, laptop');
214-
$this->assertCount(3, $tree[0]->getChildren(), 'black should have car,laptop,n/a');
213+
$this->assertCount(2, $tree[6]->getChildren(true), 'top n/a node should have car, laptop');
214+
$this->assertCount(3, $tree[0]->getChildren(true), 'black should have car,laptop,n/a');
215215
}
216216

217217
/**
@@ -222,10 +222,10 @@ public function testMultiMultiTwoLevels()
222222
$result = $this->makeResult($this->multiMultiItems);
223223
$nestedResult = new NestedResult($result);
224224
$root = $nestedResult->getRoot(2);
225-
$tree = $root->getChildren(); // nest: material, color, *
225+
$tree = $root->getChildren(true); // nest: material, color, *
226226

227227
$this->assertCount(3, $tree, '3 root nodes of material expected');
228-
$this->assertCount(1, $tree[0]->getChildren()[0]->getResultRows(), '1 metal black row expected');
228+
$this->assertCount(1, $tree[0]->getChildren(true)[0]->getResultRows(), '1 metal black row expected');
229229
}
230230

231231
/**
@@ -236,11 +236,11 @@ public function testMultiMultiTwoLevelsIndex()
236236
$result = $this->makeResult($this->multiMultiItems);
237237
$nestedResult = new NestedResult($result);
238238
$root = $nestedResult->getRoot(2, 1);
239-
$tree = $root->getChildren(); // nest: index, material, color, *
239+
$tree = $root->getChildren(true); // nest: index, material, color, *
240240

241241
$this->assertCount(3, $tree, '3 root index nodes expected');
242242
$this->assertEquals('M', $tree[0]->getValueObject()->getValue(), 'M expected');
243-
$this->assertCount(1, $tree[0]->getChildren(), '1 metal sub node under M expected');
243+
$this->assertCount(1, $tree[0]->getChildren(true), '1 metal sub node under M expected');
244244
}
245245

246246
/**
@@ -251,7 +251,7 @@ public function testSimpleIndex()
251251
$result = $this->makeResult($this->simpleItems);
252252
$nestedResult = new NestedResult($result);
253253
$root = $nestedResult->getRoot(0, 2);
254-
$tree = $root->getChildren();
254+
$tree = $root->getChildren(true);
255255

256256
$this->assertCount(2, $tree, '2 root index nodes expected');
257257
$this->assertEquals('CA', $tree[0]->getValueObject()->getValue(), 'CA(r) index expected');
@@ -269,7 +269,7 @@ public function testMultiIndex()
269269
$result = $this->makeResult($this->multiItems);
270270
$nestedResult = new NestedResult($result);
271271
$root = $nestedResult->getRoot(0, 2);
272-
$tree = $root->getChildren();
272+
$tree = $root->getChildren(true);
273273

274274
$this->assertCount(4, $tree, '4 root index nodes expected');
275275
$this->assertEquals('BL', $tree[0]->getValueObject()->getValue(), 'BL(ack|blue) index expected');

meta/AggregationList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function render($showNotFound = false)
4040
protected function renderNode(NestedValue $node)
4141
{
4242
$self = $node->getValueObject(); // null for root node
43-
$children = $node->getChildren();
43+
$children = $node->getChildren($self === null && $this->data['index']); // sort only for index
4444
$results = $node->getResultRows();
4545

4646
// all our content is in a listitem, unless we are the root node

meta/NestedValue.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,22 @@ public function addChild(NestedValue $child)
8080
/**
8181
* Get all child nodes
8282
*
83+
* @param bool $sort should children be sorted alphabetically?
8384
* @return NestedValue[]
8485
*/
85-
public function getChildren()
86+
public function getChildren($sort = false)
8687
{
8788
$children = $this->children;
88-
usort($children, [$this, 'sortChildren']);
89-
return $children;
89+
90+
if($sort) {
91+
usort($children, [$this, 'sortChildren']);
92+
} elseif(isset($children[''])) {
93+
// even when not sorting, make sure the n/a entries are last
94+
$naKids = $children[''];
95+
unset($children['']);
96+
$children[''] = $naKids;
97+
}
98+
return array_values($children);
9099
}
91100

92101
/**

0 commit comments

Comments
 (0)