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)
or
val NewList= Switches_Address.members.groupBy[state]

then iterating using the following
NewList.members.forEach[Switch_Address|
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 |
    if(!uniqueStates.contains(sw.state.toString){
        uniqueStates.put(sw.state.toString)
        uniqueItems.put(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.