Answers.Body: <p>If you're adding them all as new entries anyway, <strong>why not just sort the records before you create the entries?</strong></p>
  
  <pre><code>usort($records, array($this, 'sortCategoriesByPosition'));
  </code></pre>
  
  <p>Add that before your <code>foreach</code> loop, then...</p>
  
  <pre><code>private function sortCategoriesByPosition($a, $b)
  {
      if ($a-&gt;position == $b-&gt;position)
          return 0; 
  
      return ($a-&gt;position &lt; $b-&gt;position) ? -1 : 1;
  }
  </code></pre>
  
  <p>Stop trying to make things harder than they should be.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T04:31:31.147Z
Answers.LastActivityDate: 2014-12-10T04:31:31.147Z
Answers.UserDisplayName: Matt Stein
Body: <p>I'd like to attach children to a specific Structure entry in a specific order...</p>
  
  <blockquote>
    <ul>
    <li>Entry A</li>
    <li>Entry B
    <ul>
    <li><em>Imported Entry 2</em></li>
    <li><em>Imported Entry 1</em></li>
    <li><em>Imported Entry 3</em></li>
    </ul></li>
    <li>Entry C</li>
    </ul>
  </blockquote>
  
  <p>I've got a <code>$records</code> array to turn into imported entries:</p>
  
  <pre><code>array(
      'name'      =&gt; 'Whatever',
      'updated_at' =&gt; '2014-02-27T04:14:29Z',
      'position'   =&gt; 1, // int representing the sort order
      [...]
  )
  </code></pre>
  
  <p><strike>As I'm looping through my array to create new Craft Structure Entries, I need to be able to define the parent (Entry B), I'm just not sure how.</strike> I've <a href="https://buildwithcraft.com/classreference/controllers/EntriesController#actionSaveEntry-detail" rel="nofollow">figured out how</a> to attach new children to <em>Entry B</em>, I just don't know how to programmatically set their order. My imagination writes code that looks like this:</p>
  
  <pre><code>$parentCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  
  $parentCriteria-&gt;search  = 'slug:entry-b';
  $parentCriteria-&gt;section = 'sectionName';
  $parentCriteria-&gt;type    = 'sectionType';
  
  $entries = $parentCriteria-&gt;find();
  $parent = $entries[0];
  
  foreach ($records as $row)
  {
      $entry = new EntryModel();
  
      $entry-&gt;sectionId = 4;
      $entry-&gt;typeId    = 4;
      $entry-&gt;parentId  = $parent-&gt;id;
      $entry-&gt;authorId  = 1;
      $entry-&gt;postDate  = $row-&gt;updated_at;
      $entry-&gt;title     = $row-&gt;name;
      $entry-&gt;sortOrder = $row-&gt;position; // ← quite impossible!
  
      $success = craft()-&gt;entries-&gt;saveEntry($entry);
  }
  </code></pre>
  
  <p>It's <code>$entry-&gt;sortOrder</code> that's completely made up—<strong>how do you attach an entry to a specific parent in a specific order?</strong></p>
  
Tags: <plugin-development><structure><relations>
Title: How can I append a structure entry and set its order?
ThreadText: # How can I append a structure entry and set its order?
  
  (Asked by: username_0 on 2014-12-10)
  
  I'd like to attach children to a specific Structure entry in a specific order...
  
  > * Entry A
  > * Entry B
  >   + *Imported Entry 2*
  >   + *Imported Entry 1*
  >   + *Imported Entry 3*
  > * Entry C
  
  I've got a `$records` array to turn into imported entries:
  
  ```
  array(
      'name'      => 'Whatever',
      'updated_at' => '2014-02-27T04:14:29Z',
      'position'   => 1, // int representing the sort order
      [...]
  )
  
  ```
  
  As I'm looping through my array to create new Craft Structure Entries, I need to be able to define the parent (Entry B), I'm just not sure how. I've [figured out how](https://buildwithcraft.com/classreference/controllers/EntriesController#actionSaveEntry-detail) to attach new children to *Entry B*, I just don't know how to programmatically set their order. My imagination writes code that looks like this:
  
  ```
  $parentCriteria = craft()->elements->getCriteria(ElementType::Entry);
  
  $parentCriteria->search  = 'slug:entry-b';
  $parentCriteria->section = 'sectionName';
  $parentCriteria->type    = 'sectionType';
  
  $entries = $parentCriteria->find();
  $parent = $entries[0];
  
  foreach ($records as $row)
  {
      $entry = new EntryModel();
  
      $entry->sectionId = 4;
      $entry->typeId    = 4;
      $entry->parentId  = $parent->id;
      $entry->authorId  = 1;
      $entry->postDate  = $row->updated_at;
      $entry->title     = $row->name;
      $entry->sortOrder = $row->position; // ← quite impossible!
  
      $success = craft()->entries->saveEntry($entry);
  }
  
  ```
  
  It's `$entry->sortOrder` that's completely made up—**how do you attach an entry to a specific parent in a specific order?**
  
  **Tags:** plugin-development, structure, relations
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-12-10. Score: 2 | Accepted answer
  
  If you're adding them all as new entries anyway, **why not just sort the records before you create the entries?**
  
  ```
  usort($records, array($this, 'sortCategoriesByPosition'));
  
  ```
  
  Add that before your `foreach` loop, then...
  
  ```
  private function sortCategoriesByPosition($a, $b)
  {
      if ($a->position == $b->position)
          return 0; 
  
      return ($a->position < $b->position) ? -1 : 1;
  }
  
  ```
  
  Stop trying to make things harder than they should be.
  
  
  ---
  
  