Repository: InsertKoinIO/koin
File: projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/InstanceRegistry.kt (Kotlin)

Code before:
    internal fun dropScopeInstances(scope: Scope) {
        val factories = _instances.values.toTypedArray()
        factories.filterIsInstance<ScopedInstanceFactory<*>>().forEach { factory -> factory.drop(scope) }
    }

    internal fun close() {
        val factories = _instances.values.toTypedArray()
        factories.forEach { factory -> factory.dropAll() }
        _instances.clear()
    }

    internal fun <T> getAll(clazz: KClass<*>, instanceContext: ResolutionContext): List<T> {
        return _instances.values
            .filter { factory ->
                (factory.beanDefinition.scopeQualifier == instanceContext.scope.scopeQualifier ||
                    factory.beanDefinition.scopeQualifier == instanceContext.scope.scopeArchetype
                ) &&
                (factory.beanDefinition.primaryType == clazz || factory.beanDefinition.secondaryTypes.contains(clazz))
            }
            .distinct()
            .mapNotNull { it.get(instanceContext) as? T }
    }

    internal fun unloadModules(modules: Set<Module>) {
        modules.forEach { unloadModule(it) }
    }

    private fun unloadModule(module: Module) {
        module.mappings.keys.forEach { mapping ->
            _instances[mapping]?.dropAll()
            _instances.remove(mapping)
        }
    }

    fun size(): Int {
        return _instances.size
    }


}


Review comment (suggestion):
The sorting by `beanDefinition.toString()` is not deterministic and could lead to inconsistent behavior. The `toString()` method typically returns memory addresses or object identities that may vary between runs. If ordering is important for correctness, consider sorting by a stable property (e.g., definition name, qualifier, or type). If ordering is only for test stability, consider documenting that intent.
```suggestion
            .sortedWith(compareBy({ it.beanDefinition.primaryType.qualifiedName ?: "" }, { it.beanDefinition.qualifier?.value ?: "" }))
```

Code after:
            val index = indexKey(clazz, def.qualifier, def.scopeQualifier)
            saveMapping(allowOverride, index, factory)
        }
    }

    internal fun dropScopeInstances(scope: Scope) {
        val factories = _instances.values.toTypedArray()
        factories.filterIsInstance<ScopedInstanceFactory<*>>().forEach { factory -> factory.drop(scope) }
    }

    internal fun close() {
        val factories = _instances.values.toTypedArray()
        factories.forEach { factory -> factory.dropAll() }
        _instances.clear()
    }

    internal fun <T> getAll(clazz: KClass<*>, instanceContext: ResolutionContext): List<T> {
        val factories = _instances.values
            .filter { factory ->
                (factory.beanDefinition.scopeQualifier == instanceContext.scope.scopeQualifier ||
                    factory.beanDefinition.scopeQualifier == instanceContext.scope.scopeArchetype
                ) &&
                (factory.beanDefinition.primaryType == clazz || factory.beanDefinition.secondaryTypes.contains(clazz))
            }
            .distinct()
            .sortedWith(compareBy({ it.beanDefinition.toString() }))
        return factories.mapNotNull { it.get(instanceContext) as? T }
    }

    internal fun unloadModules(modules: Set<Module>) {
        modules.forEach { unloadModule(it) }
    }

    private fun unloadModule(module: Module) {
        module.mappings.keys.forEach { mapping ->
            _instances[mapping]?.dropAll()
            _instances.remove(mapping)
        }
    }

    fun size(): Int {
        return _instances.size
    }


}