Re-ordering group members by state within Rules or removing duplicates

I have a set of strings stored in items within a group named Switches_Address

Sometimes I would like a list of the states with all duplicate values removed, or in an ordered list by STATE so that I can detect duplicates whilst iterating via a .

I have tried these but cant work out the correct syntax.

val NewList= Switches_Address.members.sort(state)
val NewList= Switches_Address.members.groupBy[state]

then iterating using the following
logInfo(“SwitchRules”, " Address- ‘" + Switch_Address.state + "’")

Using OH2

val NewList = Switches_Address.members.sortBy[state]

You may need to use state.toString as I don’t know if all the different State classes support Comparable. But String does which will give you the list sorted alphabetically.

If you want to remove the duplicates (i.e. only keep one of the Switches that is ON and one that is OFF, throwing out the rest) you can do something like the following:

val Set<String> uniqueStates = newImmutableSet
val List<GenericItem> uniqueItems = newArrayList

Switches_Address.members.forEach[sw |

// uniqueItems contains only the first Item who had a unique state.

Thank you for your help. This was exactly what i was looking for, although i couldnt add new items when using newImmutableSet so changed to new HashSet() which seems to work.