24 lines
1.0 KiB
Kotlin
24 lines
1.0 KiB
Kotlin
package lab_4
|
|
|
|
fun main() {
|
|
val furnitureArray = listOf(
|
|
Furniture("Chair", "Wood", 10, 5, 5, 10),
|
|
Furniture("Counter-top", "Marble", 1_000, 10, 5, 1),
|
|
Furniture("Dinner table", "Glass", 500, 15, 10, 1),
|
|
Furniture("Office table", "Wood", 200, 10, 7, 1),
|
|
Furniture("Refrigerator", "Stainless steel", 20_000, 8, 4, 10)
|
|
)
|
|
|
|
val maxNameWidth = furnitureArray.maxBy {it.name.length}.name.length
|
|
val maxMaterialWidth = furnitureArray.maxBy { it.material.length }.material.length
|
|
val maxPriceWidth = furnitureArray.maxBy { it.price }.price.toString().length
|
|
|
|
println("\nUnsorted array:")
|
|
furnitureArray.forEach { it.print(maxNameWidth, maxMaterialWidth, maxPriceWidth) }
|
|
|
|
println("\nSorted alphabetically by name:")
|
|
furnitureArray.sortedBy { it.name }.forEach { it.print(maxNameWidth, maxMaterialWidth, maxPriceWidth) }
|
|
|
|
println("\nSorted alphabetically by material:")
|
|
furnitureArray.sortedBy { it.material }.forEach { it.print(maxNameWidth, maxMaterialWidth, maxPriceWidth) }
|
|
} |