Ok so I'm wanting to sort an array based off of another array. Ok so that probably sounds pretty confusing so let me explain.
Alright so let's make an example of an app about grocery shopping. Ok so I have an array like this:
var fruitsArray = [String:Int]()
Let's say that the values of the array are
[apple:32, banana:45, grape:7, strawberry:23]
Ok so first off, I sort this array by the `Int`, descendingly, which gives us
[banana:45 apple:32, strawberry:23, grape:7]
Ok great. But now I want an array of shopping lists and each element of that array contains an array of food. So let's make a `Struct`...
struct List {
var name:String = ""
var items:[String] = [""]
init(name: String, items: [String) {
self.name = name
self.items = items
}
}
and then an array of `List`...
var shoppingLists = [List]()
finally, let's add some elements to `shoppingLists`...
[List(name: List 1, items: [banana, pizza, watermelon, apple]),
List(name: List 2, items: [cookie, water, grape, apple]),
List(name: List 3, items: [apple, strawberry, banana, cheese]),
List(name: List 4, items: [apple, strawberry, grape, watermelon])]
Ok so this is where it gets kinda difficult. I want to sort `shoppingLists` by looking at the `items` array in `shoppingLists` and comparing them with `fruitsArray`. Let me show you what I want the final product would be and then I'll explain why.
sortedShoppingList =
[List(name: List 3, items: [apple, strawberry, banana, cheese]),
List(name: List 4, items: [apple, strawberry, grape, watermelon]),
List(name: List 1, items: [banana, pizza, watermelon, apple]),
List(name: List 2, items: [cookie, water, grape, apple])]
Ok so why in the world is it sorted like this? Let's take it element by element. So List 3 is at the top because it contains 3 elements from fruitsArray. Now you might be saying list 4 also contains 3 elements from `fruitsArray` and yes that's true, but list 3 contains elements from `fruitsArray` that are higher in "ranking". Both list 3 and 4 contain [apple, strawberry] but list 3 contains `banana` while list 4 contains `grape`. `Banana` "outranksks" `grape` therefore making list 3 first. So moving on, List 1 is after list 4 because it contains [apple, banana] which is higher in "rank" than the [apple, grape] in list 2.
Alright well, I really hope that wasn't confusing. If you have any questions or need me to clarify, please ask! Thanks so much for reading this and I hope you can help me out.