<p>I was able to get help from a co-worker on this issue. Below is the code used to get Countries and Resellers to sort correctly. Krzysztof Boduch's answer was pretty solid and allowed for a great starting point.</p>

<pre><code>{% for entry in craft.entries.slug(entrySlug).find() %}
{% set resellersByCountry = {} %}
{% for block in entry.chooseResellers %}
    {% for reseller in block.chooseReseller.with(['rdCountry']) %}
        {% set countryName = reseller.rdCountry[0].title %}

        {% if resellersByCountry[countryName] is defined %}
            {% set temp = resellersByCountry[countryName] %}
            {% set temp = resellersByCountry[countryName] | merge([reseller]) %}
            {% set resellersByCountry = resellersByCountry | merge({(countryName): temp}) %}
        {% else %}
            {% set resellersByCountry = resellersByCountry | merge({(countryName): [reseller]}) %}
        {% endif %}

    {% endfor %}
{% endfor %} 

{# United States #}
{% for countryKey in resellersByCountry | keys | sort %}
    {% if countryKey == "United States" %}
    &lt;h3&gt;{{ countryKey }}&lt;/h3&gt;
    {% for reseller in resellersByCountry[countryKey] | sort %}
        &lt;p&gt;{{ reseller.title }}&lt;/p&gt;
    {% endfor %}
    {% endif %}
{% endfor %}

{# International #}
{% for countryKey in resellersByCountry | keys | sort %}
    {% if countryKey != "United States" %}
    &lt;h3&gt;{{ countryKey }}&lt;/h3&gt;
    {% for reseller in resellersByCountry[countryKey] | sort %}
        &lt;p&gt;{{ reseller.title }}&lt;/p&gt;
    {% endfor %}
{% endif %}
{% endfor %}
</code></pre>

<p>{% endfor %}</p>

CC BY-SA 3.0
2016-12-06T19:29:53.170Z
2016-12-06T19:29:53.170Z
johnHoysa
<p><strong>To achieve structure:</strong></p>

<ul>
<li><p>Country Name A</p>

<ul>
<li>Reseller C</li>
</ul></li>
<li><p>Country Name C</p>

<ul>
<li>Reseller B</li>
<li>Reseller D</li>
</ul></li>
</ul>

<p><strong>With configuration:</strong></p>

<p><em>Category</em> Countries</p>

<p><em>Channel</em> Resellers:</p>

<ul>
<li>rdCountry (Country <em>Categories</em>) field</li>
</ul>

<p><em>Channel</em> Products:</p>

<ul>
<li>chooseResellers (<em>Matrix</em>) field

<ul>
<li>block

<ul>
<li>chooseReseller (Reseller <em>Entries</em>) field</li>
</ul></li>
</ul></li>
</ul>

<p><strong>You gonna need code like this:</strong></p>

<pre><code>{% for entry in craft.entries.slug(entrySlug).find() %}
    {% set countries = {} %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller.with(['rdCountry']) %}
            {% set countryName = reseller.rdCountry[0].title %}

            {% if countries[countryName] is defined %}
                {% set temp = countries[countryName] | merge([reseller]) %}
                {% set countries = countries | merge({(countryName): temp}) %}
            {% else %}
                {% set countries = countries | merge({(countryName): [reseller]}) %}
            {% endif %}

        {% endfor %}
    {% endfor %} 

    {% for countryName, resellers in countries %}
        &lt;h3&gt;{{ countryName }}&lt;/h3&gt;
        {% for reseller in resellers %}
            &lt;p&gt;{{ reseller.title }}&lt;/p&gt;
        {% endfor %}
    {% endfor %}
{% endfor %}
</code></pre>

<hr>

<p>Because of the current section/field/relation configuration we need to use <code>countries</code> temporary array to store <code>Country</code> => <code>Reseller</code> structure. </p>

<p><code>.with(['rdCountry'])</code> is what you call <a href="https://craftcms.com/docs/templating/eager-loading-elements" rel="nofollow noreferrer">eager loading</a>. It allows you to reduce sql queries, speed up the whole process. With eager loaded <em>entries</em> field you replace <code>find()</code> with index number.</p>

<p><code>countries</code> looks like this at the end:</p>

<pre><code>{  
   "Country Name A":[  
      {  
         "title":"Reseller C"
      }
   ],
   "Country Name C":[  
      {  
         "title":"Reseller B"
      },
      {  
         "title":"Reseller D"
      }
   ]
}
</code></pre>

CC BY-SA 3.0
2016-11-23T20:11:42.750Z
Wow thank you so much for the help! You really got me a lot further then I thought someone would be able to help me. I would like to apologize for my delay in getting back to you but I was never notified that someone replied to my posting. I hope your week is going well, mine is now that you helped out!
johnHoysa
CC BY-SA 3.0
2016-11-23T21:13:59.273Z
One last question, how would I order the out put by title?
johnHoysa
CC BY-SA 3.0
2016-11-16T21:56:31.083Z
2016-11-16T22:06:08.780Z
2016-11-16T22:06:08.780Z
Krzysztof Boduch
<p>Any help would be greatly appreciated! I hope I explained my situation in enough detail to get the help that is needed.</p>

<p>Currently I have two channels that need to share information. This is going from one channel to an other.  </p>

<p>Channel One is called Resellers
This information needs to be feed into my other channel called Products.
Reseller contains info about various resellers, places to purchase a product. 
In this channel I have a field for Category. The category is actually a Country name. The rest of the fields are plain text.  </p>

<p>Channel 2 is called Products 
Products has various fields but I am interested in doing is pulling in info from Resellers and displaying it. I am able to pull in everything from Resellers except for the Category name.</p>

<p>In Products I have a matrix field that contains an entry field and a plain text field. 
The entry field pulls in the information about each reseller. 
The plain text field allows me to override a field from Reseller to display per product information. I got this part working without an issue.  </p>

<p>What I am trying to create is the following structure:
Country name then under the country list out each reseller alphabetically.
You will see Country Name A, A means nothing it just shows alphabetical listing. </p>

<ul>
<li><p>Country Name A </p>

<ul>
<li>Reseller A </li>
<li>Reseller T</li>
</ul></li>
<li><p>Country Name C</p>

<ul>
<li>Reseller D</li>
<li>Reseller H</li>
<li>Reseller R</li>
<li>Reseller T</li>
</ul></li>
</ul>

<p>Below I have listed out my field handle names that are relevant to this issue.</p>

<p>From Resellers</p>

<ul>
<li>rdCountry - field where category is selected from</li>
</ul>

<p>I am able to pull in all other fields easily that are part of Resellers.</p>

<p>From Products</p>

<ul>
<li>Matrix field - chooseResellers

<ul>
<li>Entries field - chooseReseller</li>
<li>Plain text field - customProductLink</li>
</ul></li>
</ul>

<p>Again I am able to display each reseller just fine. What I am having issues with is pulling in the Category/Country name. Once I pull in the country name I might need help with setting up my loop to display everything.</p>

<p>Thanks for your time and help, it is greatly appreciated! </p>

<p>EDIT:
So the below code seems to solve my issue listed above. Now I just gotta loop through everything correctly.</p>

<pre><code>{% for entry in craft.entries.slug(entrySlug).find() %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller %}
            &lt;h3&gt;{{ reseller.rdCountry.first() }}&lt;/h3&gt;
        {% endfor %}
    {% endfor %}{# /matrix #}
{% endfor %}{# /for product #}
</code></pre>

<p>EDIT TWO - the code solves part of the problem but I do not think will allow me to loop through everything as I was expecting. Help is appreciated! </p>

<matrix><categories><entries-field>
Matrix with an entries field and need to grab category title
# Matrix with an entries field and need to grab category title

(Asked by: username_0 on 2016-11-15)

Any help would be greatly appreciated! I hope I explained my situation in enough detail to get the help that is needed.
Currently I have two channels that need to share information. This is going from one channel to an other.  
Channel One is called Resellers
This information needs to be feed into my other channel called Products.
Reseller contains info about various resellers, places to purchase a product. 
In this channel I have a field for Category. The category is actually a Country name. The rest of the fields are plain text.  
Channel 2 is called Products 
Products has various fields but I am interested in doing is pulling in info from Resellers and displaying it. I am able to pull in everything from Resellers except for the Category name.
In Products I have a matrix field that contains an entry field and a plain text field. 
The entry field pulls in the information about each reseller. 
The plain text field allows me to override a field from Reseller to display per product information. I got this part working without an issue.  
What I am trying to create is the following structure:
Country name then under the country list out each reseller alphabetically.
You will see Country Name A, A means nothing it just shows alphabetical listing. 

Country Name A 

Reseller A 
Reseller T

Country Name C

Reseller D
Reseller H
Reseller R
Reseller T


Below I have listed out my field handle names that are relevant to this issue.
From Resellers

rdCountry - field where category is selected from

I am able to pull in all other fields easily that are part of Resellers.
From Products

Matrix field - chooseResellers


Entries field - chooseReseller
Plain text field - customProductLink


Again I am able to display each reseller just fine. What I am having issues with is pulling in the Category/Country name. Once I pull in the country name I might need help with setting up my loop to display everything.
Thanks for your time and help, it is greatly appreciated! 
EDIT:
So the below code seems to solve my issue listed above. Now I just gotta loop through everything correctly.
{% for entry in craft.entries.slug(entrySlug).find() %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller %}
            {{ reseller.rdCountry.first() }}
        {% endfor %}
    {% endfor %}{# /matrix #}
{% endfor %}{# /for product #}

EDIT TWO - the code solves part of the problem but I do not think will allow me to loop through everything as I was expecting. Help is appreciated!

**Tags:** matrix, categories, entries-field

---

## 2 answers

### Answer by username_0 on 2016-12-06. Score: 0 | Accepted answer

I was able to get help from a co-worker on this issue. Below is the code used to get Countries and Resellers to sort correctly. Krzysztof Boduch's answer was pretty solid and allowed for a great starting point.
{% for entry in craft.entries.slug(entrySlug).find() %}
{% set resellersByCountry = {} %}
{% for block in entry.chooseResellers %}
    {% for reseller in block.chooseReseller.with(['rdCountry']) %}
        {% set countryName = reseller.rdCountry[0].title %}

        {% if resellersByCountry[countryName] is defined %}
            {% set temp = resellersByCountry[countryName] %}
            {% set temp = resellersByCountry[countryName] | merge([reseller]) %}
            {% set resellersByCountry = resellersByCountry | merge({(countryName): temp}) %}
        {% else %}
            {% set resellersByCountry = resellersByCountry | merge({(countryName): [reseller]}) %}
        {% endif %}

    {% endfor %}
{% endfor %} 

{# United States #}
{% for countryKey in resellersByCountry | keys | sort %}
    {% if countryKey == "United States" %}
    {{ countryKey }}
    {% for reseller in resellersByCountry[countryKey] | sort %}
        {{ reseller.title }}
    {% endfor %}
    {% endif %}
{% endfor %}

{# International #}
{% for countryKey in resellersByCountry | keys | sort %}
    {% if countryKey != "United States" %}
    {{ countryKey }}
    {% for reseller in resellersByCountry[countryKey] | sort %}
        {{ reseller.title }}
    {% endfor %}
{% endif %}
{% endfor %}

{% endfor %}


---

### Answer by username_1 on 2016-11-16. Score: 2

To achieve structure:

Country Name A

Reseller C

Country Name C

Reseller B
Reseller D


With configuration:
Category Countries
Channel Resellers:

rdCountry (Country Categories) field

Channel Products:

chooseResellers (Matrix) field


block


chooseReseller (Reseller Entries) field



You gonna need code like this:
{% for entry in craft.entries.slug(entrySlug).find() %}
    {% set countries = {} %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller.with(['rdCountry']) %}
            {% set countryName = reseller.rdCountry[0].title %}

            {% if countries[countryName] is defined %}
                {% set temp = countries[countryName] | merge([reseller]) %}
                {% set countries = countries | merge({(countryName): temp}) %}
            {% else %}
                {% set countries = countries | merge({(countryName): [reseller]}) %}
            {% endif %}

        {% endfor %}
    {% endfor %} 

    {% for countryName, resellers in countries %}
        {{ countryName }}
        {% for reseller in resellers %}
            {{ reseller.title }}
        {% endfor %}
    {% endfor %}
{% endfor %}


Because of the current section/field/relation configuration we need to use countries temporary array to store Country => Reseller structure. 
.with(['rdCountry']) is what you call eager loading. It allows you to reduce sql queries, speed up the whole process. With eager loaded entries field you replace find() with index number.
countries looks like this at the end:
{  
   "Country Name A":[  
      {  
         "title":"Reseller C"
      }
   ],
   "Country Name C":[  
      {  
         "title":"Reseller B"
      },
      {  
         "title":"Reseller D"
      }
   ]
}

> **Comments:**
> **username_0**: Wow thank you so much for the help! You really got me a lot further then I thought someone would be able to help me. I would like to apologize for my delay in getting back to you but I was never notified that someone replied to my posting. I hope your week is going well, mine is now that you helped out!  
> **username_0**: One last question, how would I order the out put by title?  

---

