# Import participants from a CSV file, and sort them into groups

**URL:** <https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087>\
**Category:** PowerShell Help\
**Created:** [November 3, 2023, 9:33am UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087 "2023-11-03T09:33:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lansti](https://avatars.discourse-cdn.com/v4/letter/l/e9a140/32.png) [@lansti](https://forums.powershell.org/u/lansti)\
**Post date:** [November 3, 2023, 9:33am UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/1 "2023-11-03T09:33:38Z")

</div>

Hi, i have a small script, that reads player-ID, name, clubname and player rank from a CSV file:

```auto
$csvFilePath = ".\participants.csv"
$names = Import-Csv -Path $csvFilePath

```

$names is now filled with information from CSV file, then I want these names into groups, with maximun 4 participants, and minimum 3 participants:

```auto
$maxNamesPerGroup = 4
$minNamesPerGroup = 3

```

And from my CSV list, i want participants to random go to different group, so i uses:

> `$names = $names | Get-Random -Count $names.Count`

When i now execute $names, i see that the list is shuffled.

So, now i want to start from the top of the list, and create groups:

```auto
$groups = @()
$currentGroup = @()

```

But from here, I need new eyes to have a look.  
this is so far rest of my script to sort participants into different groups based on the random list i created earlier:

```auto
foreach ($name in $names) {
    $currentGroup += $name
    if ($currentGroup.Count -ge $maxNamesPerGroup) {
        $groups += $currentGroup
        $currentGroup = @()
    }
}

# Add any remaining names to the last group
if ($currentGroup.Count -ge $minNamesPerGroup) {
    $groups += $currentGroup
}

# Print the names in the groups
for ($i = 0; $i -lt $groups.Count; $i++) {
    Write-Host "Group $i :"
    $groups[$i]
}

```

But somehow, this will create one participant per Group, and if i have 12 participants in my CS V list, this script will create 12 groups…

One more thing, i also want the player with highest rank in a group to be player # 1 in that group… But i can figure that out later if someone can help me a bit on my way with getting these participants into groups.

Set of data in CSV file:

> Player-ID,Name,Club,Ranking  
> 0001,Player One,Club One,14  
> 0002,Player Two,Club Two,17  
> 0003,Player Three,Club Three,09  
> 0004,Player Four,Club Four,12  
> 0005,Player Five,Club Five,18  
> 0006,Player Six,Club Six,26  
> 0007,Player Seven,Club Seven,24  
> 0008,Player Eight,Club Eight,17  
> 0009,Player Nine,Club Nine,12  
> 0010,Player Ten,Club Ten,19  
> 0011,Player Eleven,Club Eleven,22  
> 0012,Player Twelve,Club Twelve,28

Thanks

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 3, 2023, 10:02am UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/2 "2023-11-03T10:02:28Z")

</div>

Stian,  
Welcome to the forum. 👋🏼

Before we proceed could you please go back, edit your question once agaion and fix the formatting of your code?

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

Edit:  
And you may share a set of sample data (formatted as code as well please) for us to play around with. This way we don’t have to create them by ourselfs

---

<div class="post-metadata">

**Author:** ![lansti](https://avatars.discourse-cdn.com/v4/letter/l/e9a140/32.png) [@lansti](https://forums.powershell.org/u/lansti)\
**Post date:** [November 3, 2023, 10:33am UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/3 "2023-11-03T10:33:06Z")

</div>

Sorry, i have changed a bit in my post…

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 3, 2023, 10:36am UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/4 "2023-11-03T10:36:43Z")

</div>

Looks much better now. 👍🏼

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 3, 2023, 1:29pm UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/5 "2023-11-03T13:29:25Z")

</div>

> [@lansti](#):
>
> then I want these names into groups, with maximun 4 participants, and minimum 3 participants:

Since I am quite lazy and not the smartest man on earth I usually search for solutions others may already have shared and fit to my requirements.

In this case I’d probably use one of the many solutions you can find on [StackOverflow](https://stackoverflow.com/) on “_Cutting An Array Into Slices_”.

I picked this:

> <https://stackoverflow.com/questions/35177375/better-split-arrayinchunks-for-powershell/35180666#35180666>

Now let’s digg in. First we need input data …

```auto
$NameList = @'
Player-ID,Name,Club,Ranking
0001,Player One,Club One,14
0002,Player Two,Club Two,17
0003,Player Three,Club Three,9
0004,Player Four,Club Four,12
0005,Player Five,Club Five,18
0006,Player Six,Club Six,26
0007,Player Seven,Club Seven,24
0008,Player Eight,Club Eight,17
0009,Player Nine,Club Nine,12
0010,Player Ten,Club Ten,19
0011,Player Eleven,Club Eleven,22
0012,Player Twelve,Club Twelve,28
'@ | 
ConvertFrom-Csv

```

Now you wanted to have the elements randomly grouped. So lets randomize the list first …

```auto
$RandomNameList = 
    Get-Random -InputObject $NameList -Count $NameList.Count

```

Now lets cut the randomized list into slices. Since you want to have a maximum of 4 elements per _slice_ we can simply divide the amount of elements by 4. But since we need an integer we have to make sure to always get a whole number as result  
(The original author of the function named its parameter this way. So I use the term “chunks” in this case equivalent to “_elements_” in the array or in one slice)

```auto
$NumberOfChunks = 
    [Int][Math]::Ceiling($RandomNameList.count/4)

```

Now let’s cut the whole list:

```auto
$Result = 
    Split-ArrayInChunks_UsingArrayList -inArray $RandomNameList -numberOfChunks $NumberOfChunks

```

That’s actually it. You have now an array of arrays saved to the variable `$Result`.

Ah … right …

> [@lansti](#):
>
> One more thing, i also want the player with highest rank in a group to be player # 1 in that group

That’s acvtually an easy one … since the rank is a integer you can simply sort by this porperty. Let’s pick the first _slice_ (index `0`):

```auto
$Result[0] |
    Sort-Object -Property Ranking -Descending

```

Easy huh? 😊😜😃🤷🏼‍♂️🤟🏼

---

<div class="post-metadata">

**Author:** ![lansti](https://avatars.discourse-cdn.com/v4/letter/l/e9a140/32.png) [@lansti](https://forums.powershell.org/u/lansti)\
**Post date:** [November 3, 2023, 2:14pm UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/6 "2023-11-03T14:14:04Z")

</div>

Thanks, but if I now want to output these results, i created this for loop

```auto
for ($i = 0; $i -lt $Result.Count; $i++) {
    Write-Host "Group $i :"
    $Result[$i] | Sort-Object -Property Ranking -Descending
}

```

What i get then is this result:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/5/59e1a0a6581956da392871bd2b09cf2c9e20943d.png)

But what I want, and thought my For loop would give me is groups like:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/2/216241aa10e74402fbba6d36f3fa1d8077cd0e71.png)

If I run selection $i = 0; in the For Loop, and run selection:

```auto
    Write-Host "Group $i :"
    $Result[$i] | Sort-Object -Property Ranking -Descending

```

the output looks nice, and then again run selection $i++ and run the selection aboe over again, a new group is defined… but i thought that the For loop would do that for me

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 3, 2023, 3:44pm UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/7 "2023-11-03T15:44:27Z")

</div>

I’m not sure what you’re actually trying to do … 🤷🏼‍♂️

If it’s about a nice console output try this:

```auto
$Result.GetEnumerator().Name | 
    Sort-Object |
        ForEach-Object {
            'Group # {0}' -f $_
            $Result[$_] | 
            Sort-Object -Property Ranking -Descending |
            Format-Table 
        }

```

… looks like this:

![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/2/215b3629dbf43bc5d33060b7be5fa6fcf4823bf0.png)

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 3, 2023, 4:38pm UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/8 "2023-11-03T16:38:19Z")

</div>

> [@lansti](#):
>
> ```auto
> for ($i = 0; $i -lt $Result.Count; $i++) {
> Write-Host "Group $i :"
> $Result[$i] | Sort-Object -Property Ranking -Descending
> }
> 
> ```

This is a very common issue. \*-Host commands at parse time go straight to the console where other output happens as executed. You can force the issue by adding Out-Host to the non host commands

```auto
for ($i = 0; $i -lt $Result.Count; $i++) {
    Write-Host "Group $i :"
    $Result[$i] | Sort-Object -Property Ranking -Descending | Out-Host
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/import-participants-from-a-csv-file-and-sort-them-into-groups/23087/9 "2024-05-16T20:14:32Z")

</div>


