public function testIndex() { $item1 = 'item 1'; $item2 = 'item 2'; $item3 = 'item 3'; $items = [$item1, $item2]; $list = new ArrayList($items); $index1 = $list->indexOf($item1); $this->assertEquals(0, $index1); $this->assertEquals(1, $list->indexOf($item2)); $this->assertFalse($list->indexOf($item3)); $list->removeAll($items); $list->addAll($items); $this->assertEquals(2, $list->size()); $this->assertEquals($index1, $list->indexOf($item1)); $list->add($item3, 1); $this->assertEquals($item3, $list->get(1)); $this->assertEquals($item2, $list->get(2)); }
/** * A list of tags sorted by tag-name * * @return ArrayList */ public function getSortedTags() { if ($this->comparator === null) { $this->comparator = new TagNameComparator(); } // 1) group by tag name $group = new Map(); foreach ($this->tags as $tag) { if (!$group->has($tag->getTagName())) { $group->set($tag->getTagName(), new ArrayList()); } $group->get($tag->getTagName())->add($tag); } // 2) Sort the group by tag name $group->sortKeys(new TagNameComparator()); // 3) flatten the group $sorted = new ArrayList(); foreach ($group->values() as $tags) { $sorted->addAll($tags); } return $sorted; }