'How to sort properties by class?

I'm just a beginner and learn basics, so I have such a task:

  1. Create "Animals" class and its properties
  2. Create "Plants" class and its properties
  3. Put all of it in array and sort it by name and class

I tried to do it like this:

class Animals {
    var dog: Dog? = Dog()
    var cat: Cat? = Cat()
}
class Dog {
    let dog1 = "Brungilda"
}
class Cat {
    let cat1 = "Kuzya"
}

class Plants {
    var grass: Grass? = Grass()
    var flowers: Flowers? = Flowers()
}
class Grass {
    let grass1 = "Mentha"
}
class Flowers {
    let flower1 = "Rose"
}

let animals = Animals()
let plants = Plants()

var allIn = [Any]()

allIn.append(animals.cat?.cat1)
allIn.append(animals.dog?.dog1)
allIn.append(plants.flowers?.flower1)
allIn.append(plants.grass?.grass1)

type(of: allIn[0]) // Optional <String>.Type

BUT the type of all properties is Optinal String, so I don't understand how to make it correct(2 types of properties: Animals and Plants), moreover I don't understand how to sort it by these types.

I guess my way of how to do the task is totally wrong. I'll be glad if somebody can explain me or even show the right way.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source