Question: I am trying to implement a Variant management in SAPUI5 using Personalization service of sap.ushell.Container. I have written functions to Save, Manage(delete, rename) and select Variants from the drop down. However i see strange behavior when i select a variant in the method mentioned below.

```
onSelectVariant: function (oEvent) {
    var sSelectedVariantKey = oEvent.getParameter('key');

```

Assume i have existing variants 'A1', 'A2' and 'A3'.
When i SaveAs a new variant with new values (lets call it 'X1'), the new variant is created. Then i select another already existing variant from dropdown( A1 or A2 or A3), i see the corresponding values. Now i again select the newly created variant X1 but i don't see the new values.

When i debug the above mentioned method, i see that for all the existing variants, the `oEvent.getParameter('key')` returns the variant indexs like 0,1,2,3 etc. but for the newly created variant X1, it returns the value 'sv1579082806311' and hence it doens't find it in variantset

```
oPersonalizationVariantSet.getVariant(sVariantKey)

```

and then it doesn't show the new values.

If i run the program again, i see that previously created variant X1 now shows correct values as the method `oEvent.getParameter('key')` returns the index and not 'sv....'. but if i now create a new variant X2, the same issue happens with X2.

I am running the App on cloud WebIDE and not on the FIORI launchpad.

Can someone help me what may be going wrong while saving the variant ?

Thanks

Br
Nilesh Puranik

Answer: 
A generic extension function for this, as I've required it a few times:

```
fun <T, K> Iterable<T>.groupByMany(
    keyExtractor: (T) -> Iterable<K>
): Map<K, List<T>> = mutableMapOf<K, MutableList<T>>()
    .also { grouping ->
        forEach { item ->
            keyExtractor(item).forEach { key ->
                grouping.computeIfAbsent(key) { mutableListOf() }.add(item)
            }
        }
    }

```

Usage:

```
val byTag = questions.groupByMany { it.tags }

```
```
val questions: Iterable<Question> = ....
val map = HashMap<String, MutableList<Question>>()
questions.forEach { question ->
    question.tags.forEach { tag ->
        val otherQuestions = map[tag]
        if (otherQuestions == null) map[tag] = arrayListOf(question)
        else otherQuestions.add(question)
    }
}
val resultMap: Map<String, List<Question>> = map

```